RichTextBox.SelectionLength Property

Definition

Gets or sets the number of characters selected in control.

C#
[System.ComponentModel.Browsable(false)]
public override int SelectionLength { get; set; }

Property Value

The number of characters selected in the text box.

Attributes

Examples

The following code example demonstrates how to use the SelectionLength property to determine if text is selected within the RichTextBox. This example requires that a RichTextBox control, named richTextBox1, has been added to the form. The example also requires that richTextBox1 contains text that is selected in the control.

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

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.

Applies to

Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

See also