Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

RichTextBox.SelectionColor Property

Gets or sets the text color of the current text selection or insertion point.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public Color SelectionColor { get; set; }
/** @property */
public Color get_SelectionColor ()

/** @property */
public void set_SelectionColor (Color value)

public function get SelectionColor () : Color

public function set SelectionColor (value : Color)

Property Value

A Color that represents the color to apply to the current text selection or to text entered after the insertion point.

If 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. You can use this property to change the color of text in the RichTextBox.

To make text bold in the control, use the SelectionFont property to assign a new font that has the bold font style specified.

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. 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 = 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 = 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

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.