編集

次の方法で共有


RichTextBox.SelectionColor Property

Definition

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; }
[<System.ComponentModel.Browsable(false)>]
member this.SelectionColor : System.Drawing.Color with get, set
Public Property SelectionColor As Color

Property Value

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

Attributes

Examples

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

Remarks

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.

Applies to

See also