How to Automatically Select or Highlight Text Box Upon FocusLast reviewed: June 21, 1995Article ID: Q110394 |
The information in this article applies to:
- Standard and Professional Editions of Microsoft Visual Basic for Windows, version 3.0
SUMMARYThe 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
Syntax of SelLength, SelStart, and SelText PropertiesThe SelLength, SelStart, and SelText properties apply to combo boxes and text boxes, and behave as follows:
[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:
text1.SelStart = Len(text1.Text) text1.SelLength = 1 ' Length of selectionSelLength and SelStart have a Long data type. SelText has a String data type.
|
Additional reference words: 3.00
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |