Reputation: 2009
I have annoying problem on my created userform with lengthy codes. It doesn't want to show the blinking cursor inside the textbox although the textbox is active.
So, i close the Excel App.
Open Excel App, which by default it has a blank workbook Book1
Create a sub in regular module :
Sub ShowFrmTest()
frmTest.Show vbModeless
End Sub
Then create a userform "frmTest", with one textbox "tb" and one button "btn".
Inside the userform module, i have like below :
Private Sub UserForm_Initialize()
btn_Click
End Sub
Private Sub btn_Click()
'style-X
tb.SetFocus
Debug.Print ActiveControl.Name
End Sub
then I run the ShowFrmTest sub.
result-A
The debug.print result show "tb".
So, tb is active, but I don't understand why it doesn't show the blinking cursor inside the tb.
So, I try like this for the btn_Click sub:
Private Sub UserForm_Initialize()
btn_Click
End Sub
Private Sub btn_Click()
'style-Y
btn.SetFocus
Debug.Print ActiveControl.Name
End Sub
then I run the ShowFrmTest sub.
result-B
the debug.print result show "btn",
and the btn is active because i see the button has a shadow.
Next, i try like below for the btn_Click sub:
Private Sub UserForm_Initialize()
btn_Click
End Sub
Private Sub btn_Click()
'style-Z
btn.SetFocus
tb.SetFocus
Debug.Print ActiveControl.Name
End Sub
then I run the ShowFrmTest sub, same result like in result-A.
Next i try style X, Y and Z inside the uf_init sub and I delete the btn_Click sub.
Then I run the ShowFrmTest sub.
The same result happen just like when X, Y and Z inside the btn_Click.
Next, i make like below:
Private Sub UserForm_Initialize()
btn_Click
End Sub
Private Sub UserForm_Activate()
tb.SetFocus
Debug.Print ActiveControl.Name
End Sub
Private Sub btn_Click()
tb.SetFocus
End Sub
Then I run the ShowFrmTest sub.
The same result like result-A.
BUT.... if the UserForm_Activate like below:
Private Sub UserForm_Activate()
'style-Final
btn.SetFocus
tb.SetFocus
Debug.Print ActiveControl.Name
End Sub
After I run the ShowFrmTest sub,
finally I can get the desired result.
The debug.print result show "tb",
and the tb is in focus because I can see the blinking cursor inside the tb.
My Excel version is 2010.
My question :
Also, in the style-Final ... why it need to btn.setfocus first then tb.setfocus in order to see the blinking cursor in the tb ?
Any kind of response would be greatly appreciated.
Just now I've tried like below :
Sub ShowFrmTest()
'sub in regular module which i run
frmTest.Show vbModeless
Debug.Print frmTest.ActiveControl.Name
frmTest.tb.SetFocus
End Sub
Private Sub UserForm_Initialize()
'sub in uf module
btn_Click
End Sub
Private Sub btn_Click()
'sub in uf module
tb.SetFocus
End Sub
Because I thought "oh... maybe the tb.setfocus in btn_click can't be applied before the userform is shown on the page", So I write the code like above. I still don't see the blinking cursor inside tb although the debug.print result say that "tb" is the active control name.
Upvotes: 0
Views: 149
Reputation: 103
try adjusting your userform intialize to this:
Private Sub UserForm_Initialize()
tb.Enabled = True 'or TextBox1.Enabled = True
btn_Click
End Sub
Upvotes: 0