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
)
public void Select (
	int start, 
	int length
)
public function Select (
	start : int, 
	length : int
)
Not applicable.

Parameters

start

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

length

The number of characters to select.

Exception typeCondition

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.

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

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.get_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());
    }
} //SelectMyString

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

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2015 Microsoft