RichTextBox.SelectionColor 屬性

定義

取得或設定目前文字選取範圍或插入點的文字色彩。

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

屬性值

Color,表示套用於目前文字選取範圍或輸入於插入點後文字的色彩。

屬性

範例

下列程式碼範例會向使用者顯示 ColorDialog ,以指定控制項中目前插入點之後輸入的目前文字選取範圍或文字的 RichTextBox 色彩。 這個範例需要將範例中定義的 方法新增至 Form 包含 RichTextBox 名為 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.Empty 回 。 如果目前未選取任何文字,則此屬性中指定的文字色彩會套用至目前的插入點,以及插入點之後輸入控制項的所有文字。 文字色彩設定會套用到屬性變更為不同的色彩,或直到插入點移至控制項內的不同區段為止。

如果在控制項內選取文字,則選取的文字和在文字選取範圍之後輸入的任何文字都會套用此屬性的值。 您可以使用這個屬性來變更 中的 RichTextBox 文字色彩。

若要在控制項中將文字設為粗體,請使用 SelectionFont 屬性來指派指定粗體字型樣式的新字型。

適用於

另請參閱