为什么我的 .setfocus 被忽略? [英] Why is my .setfocus ignored?

查看:43
本文介绍了为什么我的 .setfocus 被忽略?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有文本框的 Access 表单,用于重复输入数字、按 Enter 键并让脚本执行操作.为了速度,该字段应该在 DoStuff() 完成后保持焦点.

I have an Access form with a textbox that is meant to allow for repeatedly typing a number, hitting enter, and letting a script do stuff. For speed, the field should keep the focus after DoStuff() is done.

然而,虽然我确定 DoStuff() 已运行,但焦点总是转到 Tab 键顺序中的下一个字段.就像 Me.MyFld.SetFocus 被忽略了一样.

However, while I'm sure that DoStuff() is run, the focus always goes to the next field in the tab order. It's like Me.MyFld.SetFocus is being ignored.

DoStuff() 完成后如何将焦点保持在该字段上?

How do I keep the focus on this field after DoStuff() is done?

Private Sub MyFld_KeyDown(KeyCode As Integer, Shift As Integer)  
     If KeyCode = vbKeyReturn Then  
         DoStuff  
         Me.MyFld.SetFocus  
     End If
End Sub

推荐答案

如果您查看 改变焦点的按键事件的顺序,你可以看到它总是遵循这个模式:

If you look at the order of events for a keypress that would change focus, you can see that it always follows this pattern:

KeyDown → BeforeUpdate → AfterUpdate → Exit → LostFocus

你可以在那里的任何地方重新设置焦点,它仍然会遵循模式.所以我们需要告诉它停止遵循这种模式.将您的 Me.MyFld.SetFocus 替换为 DoCmd.CancelEvent,它应该可以解决您的问题.基本上,这只会让您摆脱上述模式,因此 ExitLostFocus 事件永远不会触发...

You can re-set the focus anywhere in there and it will still keep following the pattern. So we need to tell it to stop following the pattern. Replace your Me.MyFld.SetFocus with DoCmd.CancelEvent and it should fix your problem. Basically, this just kicks you out of the above pattern, so the Exit and LostFocus events never fire...

这篇关于为什么我的 .setfocus 被忽略?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆