次の方法で共有


RichTextBox.SelectionLength プロパティ

コントロール内で選択されている文字の数を取得または設定します。

Overrides Public Property SelectionLength As Integer
[C#]
public override int SelectionLength {get; set;}
[C++]
public: __property int get_SelectionLength();public: __property void set_SelectionLength(int);
[JScript]
public override function get SelectionLength() : int;public override function set SelectionLength(int);

プロパティ値

テキスト ボックス内で選択されている文字の数。

解説

このプロパティを使用すると、選択されたテキストに対して操作を実行する前に、テキスト ボックス コントロールで文字が選択されているかどうかを判断できます。また、このプロパティを使用して、for ループ内の単一文字タスクを実行するときに、選択されている文字の合計数 (空白も含む) も判断できます。

使用例

[Visual Basic, C#, C++] SelectionLength プロパティを使用して、 RichTextBox 内でテキストが選択されているかどうかを確認する方法を次の例に示します。この例は、 richTextBox1 という名前の RichTextBox コントロールがフォームに追加されていることを前提にしています。また、コントロールで選択されているテキストが richTextBox1 に含まれていることも前提となっています。

 
Private Sub WriteTextToRichTextBox()
   ' Clear all text from the RichTextBox;
   richTextBox1.Clear()
   ' Set the font for the opening text to a larger Arial font;
   richTextBox1.SelectionFont = New Font("Arial", 16)
   ' Assign the introduction text to the RichTextBox control.
   RichTextBox1.SelectedText = "The following is a list of bulleted items:" + ControlChars.Cr
   ' Set the Font for the first item to a smaller size Arial font.
   richTextBox1.SelectionFont = New Font("Arial", 12)
   ' Specify that the following items are to be added to a bulleted list.
   richTextBox1.SelectionBullet = True
   ' Set the color of the item text.
   richTextBox1.SelectionColor = Color.Red
   ' Assign the text to the bulleted item.
   richTextBox1.SelectedText = "Apples" + ControlChars.Cr
   ' Apply same font since font settings do not carry to next line.
   richTextBox1.SelectionFont = New Font("Arial", 12)
   richTextBox1.SelectionColor = Color.Orange
   richTextBox1.SelectedText = "Oranges" + ControlChars.Cr
   richTextBox1.SelectionFont = New Font("Arial", 12)
   richTextBox1.SelectionColor = Color.Purple
   richTextBox1.SelectedText = "Grapes" + ControlChars.Cr
   ' End the bulleted list.
   richTextBox1.SelectionBullet = False
   ' Specify the font size and string for text displayed below bulleted list.
   richTextBox1.SelectionFont = New Font("Arial", 16)
   richTextBox1.SelectedText = "Bulleted Text Complete!"
End Sub

[C#] 
private void ModifySelectedText()
{
   // Determine if text is selected in the control.
   if (richTextBox1.SelectionLength > 0)
   {
      // Set the color of the selected text in the control.
      richTextBox1.SelectionColor = Color.Red;
      // Set the font of the selected text to bold and underlined.
      richTextBox1.SelectionFont = new Font("Arial",10,FontStyle.Bold | FontStyle.Underline);
      // Protect the selected text from modification.
      richTextBox1.SelectionProtected = true;
   }
}

[C++] 
private:
    void ModifySelectedText()
    {
        // Determine if text is selected in the control.
        if (richTextBox1->SelectionLength > 0)
        {
            // Set the color of the selected text in the control.
            richTextBox1->SelectionColor = Color::Red;
            // Set the font of the selected text to bold and underlined.
            richTextBox1->SelectionFont =
                new System::Drawing::Font(S"Arial",10,static_cast<FontStyle>(FontStyle::Bold | FontStyle::Underline));
            // Protect the selected text from modification.
            richTextBox1->SelectionProtected = true;
        }
    }

[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 名前空間 | SelectionStart | SelectedText