Export (0) Print
Expand All

RichTextBox.SelectionLength Property

Gets or sets the number of characters selected in control.

[Visual Basic]
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);

Property Value

The number of characters selected in the text box.

Remarks

You can use this property to determine if any characters are currently selected in the text box control before performing operations on the selected text. You can also use this property to determine the total number of characters (including spaces) that are selected when performing single character tasks in a for loop.

Example

[Visual Basic, C#, C++] The following example demonstrates how to use the the SelectionLength property to determine if text is selected within the RichTextBox. This example assumes that a RichTextBox control, named richTextBox1, has been added to the form. The example also assumes that richTextBox1 contains text that is selected in the control.

[Visual Basic] 
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] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

RichTextBox Class | RichTextBox Members | System.Windows.Forms Namespace | SelectionStart | SelectedText

Show:
© 2014 Microsoft