TextBox1.GotFocus
我在事件函数的第一行有一个断点。
当我TextBox1.SetFocus
在其他地方打电话时,GotFocus
断点永远不会被击中。为什么?
调用函数中的代码: Text1.SetFocus
Private Sub Text1_GotFocus()
// code here
End Sub
一个小程序来说明我的意思。
运行程序并单击表单(标题仍为 1),单击 Text2 使其成为焦点,然后再次单击表单(标题更改为 2)
然后在 Form_Click 中未注释 Text2.SetFocus 时执行相同操作
这是代码:
'1 form with
' textbox : name=Text1 tabindex=0
' textbox : name=Text2 tabindex=1
Option Explicit
Private Sub Form_Click()
'uncomment the following line to make it work
' Text2.SetFocus
'with just the following call this wont work
Text1.SetFocus
End Sub
Private Sub Text1_GotFocus()
'increase the number in the form caption to show text1 got the focus again
Caption = CStr(Val(Caption) + 1)
End Sub
当程序启动时 Text1 获得焦点(tabindex = 0),因此当您单击表单时表单标题变为 1 没有任何变化,因为 Text1 已经具有焦点并且当您第一次单击 Text2 时不会“获取”它然后单击表单标题增加的表单
通过使用 Text2.SetFocus 取消注释该行,您可以让程序始终将焦点移到 Text2(如果它不存在),然后再将焦点移到 Text1,因此 Text1 将始终“重新获得”焦点
不过要小心!因为首先将焦点放在另一个控件上可能会产生一些您可能不想要的新事件!