次の方法で共有


RichTextBox.SelectionColor プロパティ

現在選択されているテキストまたはカーソル位置のテキストの色を取得または設定します。

Public Property SelectionColor As Color
[C#]
public Color SelectionColor {get; set;}
[C++]
public: __property Color get_SelectionColor();public: __property void set_SelectionColor(Color);
[JScript]
public function get SelectionColor() : Color;public function set SelectionColor(Color);

プロパティ値

現在選択されているテキストまたはカーソル位置以降に入力されるテキストに適用される色を表す Color

解説

現在選択されているテキストに複数の色が指定されている場合、このプロパティは Color.Empty を返します。テキストが現在選択されていない場合、このプロパティに指定されたテキストの色は、現在のカーソル位置と、カーソル位置以降にコントロールに入力されるすべてのテキストに適用されます。プロパティの値が異なる色に変更されるまで、またはカーソル位置をコントロール内の別の場所に移動するまでの間は、このテキストの色の設定が適用されます。

コントロール内でテキストが選択されている場合は、選択されているテキスト、およびテキストを選択した後に入力したすべてのテキストに対して、このプロパティの値が適用されます。このプロパティを使用して、 RichTextBox 内のテキストの色を変更できます。

コントロール内のテキストを太字にするには、 SelectionFont プロパティを使用して、太字フォント スタイルが指定されている新しいフォントを割り当てます。

使用例

[Visual Basic, C#, C++] RichTextBox コントロールで現在選択されているテキストの色、またはカーソル位置以降に入力されるテキストの色を指定するための ColorDialog をユーザーに表示する例を次に示します。この例は、コード内で定義されているメソッドが、 richTextBox1 という名前の RichTextBox コントロールを保持している Form クラスに追加されていることを前提にしています。

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


[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;
   }
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

RichTextBox クラス | RichTextBox メンバ | System.Windows.Forms 名前空間 | SelectionFont | Select