RichTextBox.SelectionLength Property

Gets or sets the number of characters selected in control.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public override int SelectionLength { get; set; }
/** @property */
public int get_SelectionLength ()

/** @property */
public void set_SelectionLength (int value)

public override function get SelectionLength () : int

public override function set SelectionLength (value : int)

Not applicable.

Property Value

The number of characters selected in the text box.

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.

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.

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

private void ModifySelectedText()
{
    // Determine if text is selected in the control.
    if (richTextBox1.get_SelectionLength() > 0) {
        // Set the color of the selected text in the control.
        richTextBox1.set_SelectionColor(Color.get_Red());
        // Set the font of the selected text to bold and underlined.
        richTextBox1.set_SelectionFont(new Font("Arial", 10, FontStyle.
            Bold | FontStyle.Underline));
        // Protect the selected text from modification.
        richTextBox1.set_SelectionProtected(true);
    }
} //ModifySelectedText

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0
Show: