Hey, i need a text box to only highlight if it is clicked, or if enter is pressed, and then deselect when enter has been press again or the mouse clicks anywhere but the textbox thanks!
Printable View
Hey, i need a text box to only highlight if it is clicked, or if enter is pressed, and then deselect when enter has been press again or the mouse clicks anywhere but the textbox thanks!
Is this what you are looking for?
As far as Enter key goes, I'll leave that to you. You'll want to use its KeyPress event or KeyDown event.Code:Private Sub Text1_GotFocus()
Text1.SelStart = 0
Text1.SelLength = Len(Text1.Text)
End Sub
Private Sub Text1_LostFocus()
Text1.SelLength = 0
End Sub
In either case, check the textbox's .SelLength and if > 0 then stuff is highlighted.
Notes:
1. When a textbox loses focus, you can't see any selection unless the HideSelection property is set to False.
2. When text is highlighted and you press a key under normal conditions, what was highlighted is lost and replaced with that key stroke. Ensure you address this when handling the Enter key by setting to zero, the KeyAscii or KeyCode parameter (depending on which event you'll trap the enter key in). Enter key doesn't erase text in single-line textbox mode.