How to Automatically Select or Highlight Text Box Upon Focus

Last reviewed: June 21, 1995
Article ID: Q110394
The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic for

  Windows, version 3.0

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:

  • 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 reference words: 3.00
KBCategory: kbprg kbcode
KBSubcategory: PrgCtrlsStd


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.