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

更新:2007 年 11 月

Windows Form RichTextBox 控制項的常見用法之一是使用屬性格式化文字,例如字型選項或段落樣式。您的應用程式可能需要記錄文字格式的任何變更以便顯示工具列,就像許多文書處理應用程式的做法一樣。

若要回應格式屬性 (Attribute) 中的變更

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

    以下範例假設含有 RichTextBox 控制項和含有工具列按鈕的 ToolBar 控制項之表單。如需工具列和工具列按鈕的詳細資料,請參閱 HOW TO:將按鈕加入至 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;
          }
       }
    

請參閱

參考

SelectionChanged

RichTextBox

其他資源

RichTextBox 控制項 (Windows Form)

在 Windows Form 上使用的控制項