共用方式為


如何:判斷 Windows Form RichTextBox 控制項中的格式屬性何時變更

Windows Forms RichTextBox 控制項的常見用法是使用字型選項或段落樣式等屬性來格式化文字。 您的應用程式可能需要追蹤文字格式設定的任何變更,以便顯示工具列,如同許多文字處理應用程式一樣。

若要回應格式化屬性中的變更

  1. SelectionChanged 事件處理常式中撰寫程式碼,根據屬性的值執行適當的動作。 下列範例會根據 SelectionBullet 屬性的值,變更工具列按鈕的外觀。 只有在控制項中移動插入點時,才會更新工具列按鈕。

    下列範例假設表單具有 RichTextBox 控制項,以及包含工具列按鈕的 ToolBar 控制項。 如需工具列和工具列按鈕的詳細資訊,請參閱如何:將按鈕新增至工具列控制項

    ' The following code assumes the existence of a toolbar control  
    ' with at least one toolbar button.  
    Private Sub RichTextBox1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RichTextBox1.SelectionChanged  
       If RichTextBox1.SelectionBullet = True Then  
          ' Bullet button on toolbar should appear pressed  
          ToolBarButton1.Pushed = True  
       Else  
           ' Bullet button on toolbar should appear unpressed  
           ToolBarButton1.Pushed = False  
       End If  
    End Sub  
    
    // The following code assumes the existence of a toolbar control  
    // with at least one toolbar button.  
    private void richTextBox1_SelectionChanged(object sender,  
    System.EventArgs e)  
    {  
       if (richTextBox1.SelectionBullet == true)
       {  
          // Bullet button on toolbar should appear pressed  
          toolBarButton1.Pushed = true;  
       }  
       else
       {  
          // Bullet button on toolbar should appear unpressed  
          toolBarButton1.Pushed = false;  
       }  
    }  
    
    // The following code assumes the existence of a toolbar control  
    // with at least one toolbar button.  
    private:  
       System::Void richTextBox1_SelectionChanged(  
          System::Object ^  sender, System::EventArgs ^  e)  
       {  
          if (richTextBox1->SelectionBullet == true)  
          {  
             // Bullet button on toolbar should appear pressed  
             toolBarButton1->Pushed = true;  
          }  
          else  
          {  
             // Bullet button on toolbar should appear unpressed  
             toolBarButton1->Pushed = false;  
          }  
       }  
    

另請參閱