如何:确定 Windows 窗体 RichTextBox 控件中的格式设置属性何时更改
更新:2007 年 11 月
Windows 窗体 RichTextBox 控件的通常用途是使用字体选项或段落样式等属性来格式化文本。如许多字处理应用程序一样,您的应用程序可能需要跟踪文本格式设置的任何更改,以便显示工具栏。
响应格式属性的更改
在 SelectionChanged 事件处理程序中编写代码以根据属性的值来执行合适的操作。下面的示例根据 SelectionBullet 属性的值来更改工具栏按钮的外观。只有当插入点在控件中移动时,才会更新工具栏按钮。
下面的示例假定窗体具有一个 RichTextBox 控件和一个包含工具栏按钮的 ToolBar 控件。有关工具栏以及工具栏按钮的更多信息,请参见 如何:向 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; } }