This article was previously published under Q110394
This article has been archived. It is offered "as is" and will no longer be updated.
The sample program below automatically selects (highlights) all text in atext box whenever the text box gets the focus. This is done by using theSelStart and SelLength properties in the GetFocus event for the text box.
Automatic highlighting is useful in data entry boxes where you want to givethe user the option to quickly overwrite the existing contents by pressingany character key.
To avoid overwriting the highlighted text after giving focus to the textbox, the user can single-click the text, or press an arrow (direction) keyor cursor-movement key (such as END or HOME) to remove the highlightingfrom the text and allow editing.
Example How to Automatically Select Text Whenever Given the Focus
Start a new project in Visual Basic. Form1 is created by default.
Add two text boxes to Form1.
Add the following code to the Form Load event:
Sub Form_Load () text1.Text = "This sentence is highlighted whenever given the focus." text2.Text = "This is not highlighted." End Sub
Add the following code to the Text1 GotFocus event:
Sub Text1_GotFocus () text1.SelStart = 0 ' Start selection at beginning. text1.SelLength = Len(text1.Text) ' Length of text in Text1. End Sub
Start the program, or press the F5 key. Click the text in Text1 to remove its highlighting. Click Text2 to change the focus. Click Text1 again and notice that the complete contents of Text1 are automatically highlighted again. Close the form to end the program.
Syntax of SelLength, SelStart, and SelText Properties
The SelLength, SelStart, and SelText properties apply to combo boxes andtext boxes, and behave as follows:
SelLength determines the number of characters selected.
SelStart determines the starting point of text selected. SelStart indicates the position of the insertion point if no text is currently selected.
SelText determines the string containing the currently selected text; consists of an empty string ("") if no characters are currently selected.
The SelLength, SelStart, SelText properties are not available at designtime. They are only available at run time. They have the following syntax:
For SelLength and SelStart, the valid range of settings is 0 to the textlength -- the total number of characters in the edit area of a combo box ortext box.
Use these properties for tasks such as setting the insertion point,establishing an insertion range, selecting substrings in a control, orclearing text. Used in conjunction with the Clipboard object, theseproperties are useful for copy, cut, and paste operations. When workingwith these properties:
Setting SelLength less than 0 causes a run-time error.
Setting SelStart greater than the text length sets the property to the existing text length; changing SelStart changes the selection to an insertion point and sets SelLength to 0.
Setting SelText to a new value sets SelLength to 0 and replaces the selected text with the new string.
For example, to position the insertion point at the end of a text boxwhenever the box gets the focus, use the following code:
text1.SelStart = Len(text1.Text) text1.SelLength = 1 ' Length of selection
SelLength and SelStart have a Long data type. SelText has a String datatype.