Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Visual Basic
Expertise: Beginner
Jul 30, 1997

AutoSelect Text Box Contents

Users often find it faster to retype the entire contents of a text box rather than position the cursor within the data, and then edit the data. This is especially true if the length of the data is short or if the data isn't visible, as with a password field, for example. Double-clicking or using a mouse to select the control's contents is slow and inconvenient. I created this small routine to automatically select all data within a control. I place this routine in a code module so that it's accessible from all forms. I call this routine from a control's GotFocus event. This way, the data is selected if the user tabs to or clicks on the control, or if a data validation routine does a SetFocus:
 
Private Sub MyTextBox_GotFocus()
	AutoSelect MyTextBox
End Sub

The AutoSelect routine is quite simple:

Sub AutoSelect(SelObject As Control)
' The AutoSelect routine "selects" the 
' control's entire contents as if it were
' doubled-clicked.
	SelObject.SelStart = 0
	If TypeOf SelObject Is MaskEdBox Then
		SelObject.SelLength = Len(SelObject.FormattedText)
	Else
		If TypeOf SelObject Is TextBox Then
			SelObject.SelLength = Len(SelObject.Text)
		End If
	End If
End Sub
Kevin Forth
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date