How to Automatically Select or Highlight Text Box Upon Focus

ID: Q110394


The information in this article applies to:


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.


MORE INFORMATION

Example How to Automatically Select Text Whenever Given the Focus

  1. Start a new project in Visual Basic. Form1 is created by default.


  2. Add two text boxes to Form1.


  3. 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
     


  4. 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
     


  5. 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:
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:
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          : 
Version           : 
Platform          : 
Issue type        : 

Last Reviewed: June 23, 1999