如何确定 VBA UserForm 中的下一个制表位?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1855096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 11:04:38  来源:igfitidea点击:

How to determine next tabstop in VBA UserForm?

vba

提问by Gary McGill

I have a UserForm with some textbox entry fields on it that are enabled/disabled by a checkbox. When a checkbox is clicked to check it, I'd like to move the focus into the now-enabled textbox.

我有一个用户窗体,上面有一些由复选框启用/禁用的文本框输入字段。当单击复选框进行检查时,我想将焦点移到现在启用的文本框中。

The textbox is the next control after the checkbox in the tab order, so it seems like using the tab order to find the appropriate textbox would be a good idea.

文本框是选项卡顺序中复选框之后的下一个控件,因此使用选项卡顺序查找适当的文本框似乎是一个好主意。

But... how can I find the next control in the tab order after a given control? Is there a method to do that, or do I have to enumerate allthe controls and figure it out for myself?

但是...如何在给定控件之后按 Tab 键顺序找到下一个控件?有没有办法做到这一点,还是我必须枚举所有控件并自己弄清楚?

回答by mikemay

I appreciate that this comes under the heading of "enumerating all the controls" but it's pretty simple and I attach the code for completeness:

我很欣赏这在“枚举所有控件”的标题下,但它非常简单,我附上代码以确保完整性:

Private Sub CheckBox1_Click()
    Dim ctl As Control
    For Each ctl In Me.Controls
        If ctl.TabIndex = Me.ActiveControl.TabIndex + 1 Then
            ctl.SetFocus
            Exit For
        End If
    Next
End Sub

回答by Adriaan Stander

As a different way of looking at this.

作为看待这个的不同方式。

Can you not rather use the textbox you want focussed, and set that name in the checkbox.tag

您是否可以不使用您想要聚焦的文本框,并在 checkbox.tag 中设置该名称

then in you vba code use

然后在你的 vba 代码中使用

DoCmd.GoToControl CheckBox.Tag

Where the CheckBox.Tagis the Textbox.Name?

CheckBox.Tag在哪里是Textbox.Name

EDIT:

编辑:

OK, I found this

好的,我找到了这个

SendKeys "{Enter}", True

at VBA code for moving to next control? It must be eeeasy

用于移动到下一个控件的 VBA 代码中?一定很容易

回答by BiggerDon

I had trouble with

我遇到了麻烦

SendKeys "{Enter}", True

With a little experimentation I found this works

通过一些实验,我发现这有效

SendKeys "{TAB}", True

One caveat...if you're in the VBE stepping through the code, and watching it on the form, SendKeys is executed is executed in the code. Confused the heck out of me at first why my code started to look odd, e.g. extra spacing and extra lines!

一个警告...如果您在 VBE 中逐步执行代码,并在表单上查看它,则执行 SendKeys 是在代码中执行的。起初我很困惑为什么我的代码开始看起来很奇怪,例如额外的间距和额外的行!

回答by Statsjockey

With all due respect to the kind advice offered thus far, this issue shouldn't be addressed through programming when there's a way to address it within the userform itself.

考虑到迄今为止提供的善意建议,当有一种方法可以在用户表单本身内解决这个问题时,不应通过编程来解决这个问题。

Click on the userform itself, then right click and select "tab order". You can then move each element within the userform to whatever position you want without having to resort to complicated and unstable programming tricks.

单击用户表单本身,然后右键单击并选择“标签顺序”。然后,您可以将用户表单中的每个元素移动到您想要的任何位置,而不必求助于复杂和不稳定的编程技巧。