RichTextBox.SelectionColor 属性

定义

获取或设置当前选定文本或插入点的文本颜色。Gets or sets the text color of the current text selection or insertion point.

public:
 property System::Drawing::Color SelectionColor { System::Drawing::Color get(); void set(System::Drawing::Color value); };
[System.ComponentModel.Browsable(false)]
public System.Drawing.Color SelectionColor { get; set; }
member this.SelectionColor : System.Drawing.Color with get, set
Public Property SelectionColor As Color

属性值

表示应用到当前选定文本或在插入点后输入的文本的颜色的 ColorA Color that represents the color to apply to the current text selection or to text entered after the insertion point.

属性

示例

下面的代码示例向用户显示 ColorDialog,以指定当前选定文本或在 RichTextBox 控件中的当前插入点后输入的文本的颜色。The following code example displays a ColorDialog to the user to specify a color for the current text selection or text entered after the current insertion point in a RichTextBox control. 此示例要求将示例中定义的方法添加到 Form 类,该类包含一个名为 richTextBox1RichTextBox 控件。This example requires that the method defined in the example is added to a Form class that contains a RichTextBox control named richTextBox1.

public:
   void ChangeMySelectionColor()
   {
      ColorDialog^ colorDialog1 = gcnew ColorDialog;
      
      // Set the initial color of the dialog to the current text color.
      colorDialog1->Color = richTextBox1->SelectionColor;
      
      // Determine if the user clicked OK in the dialog and that the color has changed.
      if ( colorDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK &&
         colorDialog1->Color != richTextBox1->SelectionColor )
      {
         // Change the selection color to the user specified color.
         richTextBox1->SelectionColor = colorDialog1->Color;
      }
   }
public void ChangeMySelectionColor()
{
   ColorDialog colorDialog1 = new ColorDialog();

   // Set the initial color of the dialog to the current text color.
   colorDialog1.Color = richTextBox1.SelectionColor;

   // Determine if the user clicked OK in the dialog and that the color has changed.
   if(colorDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK && 
      colorDialog1.Color != richTextBox1.SelectionColor)
   {
      // Change the selection color to the user specified color.
      richTextBox1.SelectionColor = colorDialog1.Color;
   }
}

Public Sub ChangeMySelectionColor()
    Dim colorDialog1 As New ColorDialog()
    
    ' Set the initial color of the dialog to the current text color.
    colorDialog1.Color = richTextBox1.SelectionColor
    
    ' Determine if the user clicked OK in the dialog and that the color has
    ' changed.
    If (colorDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK) _
        And Not(colorDialog1.Color.Equals(richTextBox1.SelectionColor)) Then
        
        ' Change the selection color to the user specified color.
        richTextBox1.SelectionColor = colorDialog1.Color
    End If
End Sub

注解

如果当前选定文本具有多个指定的颜色,则此属性将返回 Color.EmptyIf the current text selection has more than one color specified, this property returns Color.Empty. 如果当前未选择任何文本,则在此属性中指定的文本颜色将应用于当前插入点和插入点之后键入到控件中的所有文本。If no text is currently selected, the text color specified in this property is applied to the current insertion point and to all text that is typed into the control after the insertion point. 在将属性更改为其他颜色或将插入点移动到控件内的其他部分之前,将应用文本颜色设置。The text color setting applies until the property is changed to a different color or until the insertion point is moved to a different section within the control.

如果在控件中选择文本,则选定文本和在选择文本后输入的任何文本都将应用此属性的值。If text is selected within the control, the selected text and any text entered after the text selection will have the value of this property applied to it. 您可以使用此属性更改 RichTextBox中文本的颜色。You can use this property to change the color of text in the RichTextBox.

若要使控件中的文本为粗体,请使用 SelectionFont 属性分配一个指定了粗体字样式的新字体。To make text bold in the control, use the SelectionFont property to assign a new font that has the bold font style specified.

适用于

另请参阅