Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
RichTextBox Class
 SelectionColor Property
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
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)
Visual Basic (Declaration)
<BrowsableAttribute(False)> _
Public Property SelectionColor As Color
Visual Basic (Usage)
Dim instance As RichTextBox
Dim value As Color

value = instance.SelectionColor

instance.SelectionColor = value
C#
[BrowsableAttribute(false)]
public Color SelectionColor { get; set; }
Visual C++
[BrowsableAttribute(false)]
public:
property Color SelectionColor {
    Color get ();
    void set (Color value);
}
JScript
public function get SelectionColor () : Color
public function set SelectionColor (value : Color)

Property Value

Type: System.Drawing..::.Color
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.

Visual Basic
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

C#
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;
   }
}

Visual C++
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;
      }
   }

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker