RichTextBox.SelectionColor 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定當前文字選取或插入點的文字顏色。
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 應套用於當前文字選取或插入點後輸入文字的顏色。
- 屬性
範例
以下程式碼範例顯示 a 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 屬性指派一個新字型,該字型指定為粗體字型。