Microsoft KB Archive/110394

= How to Automatically Select or Highlight Text Box Upon Focus =

Article ID: 110394

Article Last Modified on 10/29/2003

-

APPLIES TO


 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 3.0 Professional Edition

-



This article was previously published under Q110394



SUMMARY
The sample program below automatically selects (highlights) all text in a text box whenever the text box gets the focus. This is done by using the SelStart and SelLength properties in the GetFocus event for the text box.

Automatic highlighting is useful in data entry boxes where you want to give the user the option to quickly overwrite the existing contents by pressing any character key.

To avoid overwriting the highlighted text after giving focus to the text box, the user can single-click the text, or press an arrow (direction) key or cursor-movement key (such as END or HOME) to remove the highlighting from 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 and text 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 design time. They are only available at run time. They have the following syntax:

[form.]{combobox|textbox}.SelLength[ = length ] [form.]{combobox|textbox}.SelStart[ = index ] [form.]{combobox|textbox}.SelText[ = stringexpression ]

For SelLength and SelStart, the valid range of settings is 0 to the text length -- the total number of characters in the edit area of a combo box or text box.

Use these properties for tasks such as setting the insertion point, establishing an insertion range, selecting substrings in a control, or clearing text. Used in conjunction with the Clipboard object, these properties are useful for copy, cut, and paste operations. When working with 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 box whenever 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 data type.

Additional query words: 3.00

Keywords: KB110394

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.