Uhm, this is probably a silly question but....how would I make so that when a textbox gets the focus; if there is any text, it gets highlighted by the cursor. Thanks in advance.
-Jack Vinitsky
Printable View
Uhm, this is probably a silly question but....how would I make so that when a textbox gets the focus; if there is any text, it gets highlighted by the cursor. Thanks in advance.
-Jack Vinitsky
I think its something like this:
Text1.selstart = 0
Text1.sellenght = len(text1.text)
Code:Private Sub txtName_GotFocus()
txtName.SelStart = 0
txtName.SelLength = Len(txtName.Text)
End Sub
Thanks fot the help. Do I have to paste that in the got_focus event of each of text box on my form or is there another place I can put it so that it affects all of them?
-Jack Vinitsky
You would probably have to put that in all of them, unless you want to put it in a function instead.
Thanks for all the help.
-Jack Vinitsky
Actually, I've tackled this same problem you have, and if you have a lot of textboxes & multiple forms, here is one solution:
1. Array all the textboxes
2. Create the following sub: (if you put this in a module it will work for all your forms)
Public Sub Highlight()
'This highlights the text in the active textbox
With Screen.ActiveForm
If (TypeOf .ActiveControl Is TextBox) Then
.ActiveControl.SelStart = 0
.ActiveControl.SelLength = Len(.ActiveControl)
End If
End With
End Sub
3. Then in the GotFocus Event:
Textbox_GotFocus(Index as Integer)
Call Highlight
End Sub