Export (0) Print
Expand All

TextBoxBase.Select Method (Int32, Int32)

Selects a range of text in the text box.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public void Select(
	int start,
	int length
)

Parameters

start
Type: System.Int32

The position of the first character in the current text selection within the text box.

length
Type: System.Int32

The number of characters to select.

ExceptionCondition
ArgumentOutOfRangeException

The value of the start parameter is less than zero.

If you want to set the start position to the first character in the control's text, set the start parameter to 0. You can use this method to select a substring of text, such as when searching through the text of the control and replacing information.

NoteNote:

You can programmatically move the caret within the text box by setting the start parameter to the position within the text box where you want the caret to move to and set the length parameter to a value of zero (0). The text box must have focus in order for the caret to be moved.

NoteNote:

If this method is called without any parameters, an alternative method is used. This alternative method inherits from the Control class. When called, it sets the input focus to the control and selects the contents of the control. For more information, see the Control.Select method.

The following code example uses TextBox, a derived class, to search the contents of the control for the instance of the word "fox". If found, the code selects the word in the control using the Select method. This example requires that a TextBox named textBox1 has been created and its Text property contains the sentence "The quick brown fox jumps over the lazy dog."

public void SelectMyString()
 {
    // Create a string to search for the word "fox".
    String searchString = "fox";
    // Determine the starting location of the word "fox".
    int index = textBox1.Text.IndexOf(searchString, 16, 3);
    // Determine if the word has been found and select it if it was. 
    if (index != -1)
    {
       // Select the string using the index and the length of the string.
       textBox1.Select(index, searchString.Length);
    }
 }

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

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

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

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft