RichTextBox.SelectionColor 属性

获取或设置当前选定文本或插入点的文本颜色。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Property SelectionColor As Color
用法
Dim instance As RichTextBox
Dim value As Color

value = instance.SelectionColor

instance.SelectionColor = value
public Color SelectionColor { get; set; }
public:
property Color SelectionColor {
    Color get ();
    void set (Color value);
}
/** @property */
public Color get_SelectionColor ()

/** @property */
public void set_SelectionColor (Color value)
public function get SelectionColor () : Color

public function set SelectionColor (value : Color)

属性值

表示应用到当前选定文本或在插入点后输入的文本的颜色的 Color

备注

如果当前选定文本具有多种指定颜色,则此属性返回 Color.Empty。如果当前未选定任何文本,则此属性中指定的文本颜色应用到当前插入点和在此插入点后键入到控件中的所有文本。文本颜色设置一直应用到此属性更改为其他颜色或插入点移动到控件内的其他区域为止。

如果控件内已选定文本,则选定文本和在选定文本之后输入的任何文本都将应用此属性值。可以使用此属性更改 RichTextBox 中的文本颜色。

若要使控件中的文本变成粗体,请使用 SelectionFont 属性分配具有指定粗体字体样式的新字体。

示例

下面的代码示例为用户演示一个 ColorDialog,供其指定 RichTextBox 控件中当前选中文本的颜色或在当前插入点后输入的文本的颜色。此示例要求示例中定义的方法已添加到 Form 类中,其中包含一个名为 richTextBox1RichTextBox 控件。

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
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:
   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.set_Color(richTextBox1.get_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.get_Color() != richTextBox1.get_SelectionColor()) {
            // Change the selection color to the user specified color.
            richTextBox1.set_SelectionColor(colorDialog1.get_Color());
    }
} //ChangeMySelectionColor

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

RichTextBox 类
RichTextBox 成员
System.Windows.Forms 命名空间
SelectionFont
Select