AutoSelect Text Box Contents

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 MyTextBoxEnd SubThe 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 IfEnd Sub


Share the Post: