The TextBox control provides three properties for manipulating the text selected by the user: SelectedText, SelectionStart, and SelectionLength. Users can select a range of text with a click-and-drag operation, and the selected text will appear in reverse color. You can access the selected text from within your code through the SelectedText property, and its location in the control’s text through the SelectionStart and SelectionLength properties.
This property returns the selected text, enabling you to manipulate the current selection from within your code. For example, you can replace the selection by assigning a new value to the SelectedText property. To convert the selected text to uppercase, use the ToUpper method of the String class:
TextBox1.SelectedText = TextBox1.SelectedText.ToUpper
Use these two properties to read the text selected by the user on the control, or to select text from within your code. The SelectionStart property returns or sets the position of the first character of the selected text, somewhat like placing the cursor at a specific location in the text and selecting text by dragging the mouse. The SelectionLength property returns or sets the length of the selected text.
Suppose that the user is seeking the word Visual in the control’s text. The IndexOf method locates the string but doesn’t select it. The following statements select the word in the text, highlight it, and bring it into view, so that users can spot it instantly:
Dim seekString As String = "Visual"
Dim strLocation As Long
strLocation = TextBox1.Text.IndexOf(seekString)
If strLocation > 0 Then
TextBox1.SelectionStart = strLocation
TextBox1.SelectionLength = seekString.Length
These lines locate the string Visual (or any user-supplied string stored in the seekString variable) in the text and select it by setting the SelectionStart and SelectionLength properties of the TextBox control. If the located string lies outside the visible area of the control, the user must scroll the text to bring the selection into view. The TextBox control provides the ScrollToCaret method, which brings the section of the text with the cursor (the caret position) into view.
The few lines of code shown previously form the core of a text editor’s Find command. Replacing the current selection with another string is as simple as assigning a new value to the SelectedText property, and this technique provides you with an easy implementation of a Find and Replace operation.
The selected text in the TextBox does not remain highlighted when the user moves to another control or form; to change this default behavior, set the HideSelection property to False. Use this property to keep the selected text highlighted, even if another form or a dialog box, such as a Find & Replace dialog box, has the focus. Its default value is True, which means that the text doesn’t remain highlighted when the TextBox loses the focus.
Locating the Cursor Position in the Control
The SelectionStart and SelectionLength properties always have a value even if no text is selected on the control. In this case, SelectionLength is 0, and SelectionStart is the current position of the pointer in the text. If you want to insert some text at the pointer’s location, simply assign it to the SelectedText property, even if no text is selected on the control.