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
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 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; } }
See Also
Reference
SelectionChanged Event
RichTextBox Class
Other Resources
RichTextBox Control (Windows Forms)
Controls to Use on Windows Forms