Question:
Can I use Gotfocus event in VBA UserForm?
2009-01-07 20:40:27 UTC
I want to selection all text length (in textbox) when it got focus.
I remember in VB, it can use gotfocus event.
But I can't use gotfocus event in VBA excel userform.
And I can't find solution for solve this problem on internet.

Or if there is no direct way, please tell me the way to find out.
Three answers:
misterpinkey
2009-01-09 09:28:31 UTC
With userforms, when a textbox is activated, the entire contents are automatically highlighted (note that when activating the textbox with a mouse click, if you click on the left-hand side of the textbox, all text will be highlighted, but if you click elsewhere in the textbox, you will enter the textbox where clicked without highlighting any text).

Also, the 'GotFocus' event is triggered as an 'Enter' event in userforms.



' The following is a 'Userform_Initialize' event that highlights all text in textbox1 upon loading the form :



Private Sub Userform_Initialize()

TextBox1.SelStart = 0

TextBox1.SelLength = Len(TextBox1)

End Sub





' The following is a 'LostFocus' event that checks if the textbox is empty, and if so, replaces the empty textbox value with the words 'Enter value.' :



Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)

If TextBox1.Value = "" Then TextBox1.Value = "Enter Value."

End Sub



' The syntax to get the focus onto a textbox would be:

Textbox1.SetFocus





MP
?
2016-11-03 14:45:33 UTC
Vba Textbox Events
VBAXLMan
2009-01-08 01:06:54 UTC
You don't need that in VBA of Excel



The textbox of VBA of Excel (fm20.dll) is automatically doing that, I mean it will select the whole text when the user press tab to jump to it



If you still need to do that, use this event



TextBox1_Enter()



Enter is the substitute of GetFofus event

In the same time

Exit is the substitute of LostFocus



VBAXLMan is here to feed your Excel needs


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...