How to: Determine When Formatting Attributes Change in the Windows Forms RichTextBox Control


A common use of the Windows Forms RichTextBox control is formatting text with attributes such as font options or paragraph styles. Your application may need to keep track of any changes in text formatting for the purpose of displaying a toolbar, as in many word-processing applications.

To respond to changes in formatting attributes

  1. Write code in the SelectionChanged event handler to perform an appropriate action depending on the value of the attribute. The following example changes the appearance of a toolbar button depending on the value of the SelectionBullet property. The toolbar button will only be updated when the insertion point is moved in the control.

    The example below assumes a form with a RichTextBox control and a ToolBar control that contains a toolbar button. For more information about toolbars and toolbar buttons, see How to: Add Buttons to a ToolBar Control.

    // 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;  
          // Bullet button on toolbar should appear unpressed  
          toolBarButton1.Pushed = false;  

RichTextBox Control
Controls to Use on Windows Forms