vba 在从另一个文本框切换时将焦点设置在用户窗体文本框上
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44440050/
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
Set focus on UserForm Textbox on tabbing from another Textbox
提问by Stan
I have a Userform in Excel. It has 6 Textboxes, they are the only controls on the Userform with TabStop set to True.
我在 Excel 中有一个用户表单。它有 6 个文本框,它们是用户窗体上唯一的 TabStop 设置为 True 的控件。
I can tab and move through the textboxes. I cannot get SetFocus
to work on Events fired when moving between the Textboxes. I can put a CommandButton on the userform with Userform16.Textbox1.Setfocus
and it works as expected to move the focus to Textbox1.
我可以在文本框之间切换并移动。SetFocus
在文本框之间移动时,我无法处理触发的事件。我可以在用户窗体上放置一个命令按钮Userform16.Textbox1.Setfocus
,它可以按预期将焦点移动到 Textbox1。
I set up a simple test event (see below) to move the textbox focus back up to TextBox1 when Textbox2 is entered. It moves focus to Textbox3 when I tab out of TextBox1.
我设置了一个简单的测试事件(见下文)以在输入 Textbox2 时将文本框焦点移回 TextBox1。当我退出 TextBox1 时,它会将焦点移到 Textbox3。
Private Sub TextBox2_Enter()
Cancel = True
UserForm16.TextBox1.SetFocus
End Sub
By putting a Stop
in the above, I can see that the event is firing as expected, but it will not allow me to control the focus the next control.
通过将 aStop
放在上面,我可以看到事件按预期触发,但它不允许我控制下一个控件的焦点。
I get the same results with or without the Cancel = True
statement in the sub.
无论是否Cancel = True
使用 sub 中的语句,我都会得到相同的结果。
回答by Siddharth Rout
I set up a simple test event (see below) to move the textbox focus back up to TextBox1 when Textbox2 is entered, it actually moves focus to Textbox3 when I tab out of TextBox1.
我设置了一个简单的测试事件(见下文)以在输入 Textbox2 时将文本框焦点移回 TextBox1,当我退出 TextBox1 时,它实际上将焦点移至 Textbox3。
You can't set focus to another control in the _Enter()
event. If you try to then the code will move focus to control which has the next TabIndex
您不能将焦点设置到_Enter()
事件中的另一个控件。如果您尝试,那么代码会将焦点移动到控制哪个具有下一个TabIndex
For example
例如
Let's say you have 5 textboxes with the following TabIndex
假设您有 5 个带有以下内容的文本框 TabIndex
TextBox1 (TabIndex 0)
TextBox2 (TabIndex 1)
TextBox3 (TabIndex 3)
TextBox4 (TabIndex 4)
TextBox5 (TabIndex 2)
Now if you have this code
现在如果你有这个代码
Private Sub TextBox2_Enter()
TextBox3.SetFocus
End Sub
The moment you press TABfrom TextBox1
, the focus will move to TextBox5
(and not TextBox3
) as it has the next TabIndex
.
当您按下TABfrom 时TextBox1
,焦点将移至TextBox5
(而不是TextBox3
),因为它具有下一个TabIndex
。
Also Cancel = True
will not have any effect because it is not an argument of _Enter()
like it is of say Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
也Cancel = True
不会有任何影响,因为它不是一个参数_Enter()
像它说的Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Edit
编辑
BTW, the only time the focus will come back to Textbox1
in your scenario is when there are only two TextBoxes
in the form.
顺便说一句,Textbox1
在您的场景中唯一一次将焦点返回到TextBoxes
表单中只有两个时。
回答by zoom38
I know this is old but this answered my question and I can't comment. However, Stan in reference to your comment on June 8, 2017 I believe you were looking for something like the code below which will highlight the text in the text box when you use it with Cancel = True. I use it in the textbox exit event. This will be the indication to the user that the text box is selected.
我知道这很旧,但这回答了我的问题,我无法发表评论。但是,Stan 参考您在 2017 年 6 月 8 日的评论,我相信您正在寻找类似下面的代码,当您将其与 Cancel = True 一起使用时,它将突出显示文本框中的文本。我在文本框退出事件中使用它。这将指示用户选择了文本框。
With Me.TextBox1
.Value = "Full Name"
.SelStart = 0
.SelLength = Len(.Text)
.SetFocus
End With
Cancel = True
回答by Adam Kruzics
While it is true that _Enter()
event can not handle .SetFocus
, the _KeyDown()
event can! And thats a pretty good workaround, you just need to monitor if the TAB key was pressed.
虽然_Enter()
事件无法处理.SetFocus
,但_KeyDown()
事件可以!这是一个很好的解决方法,您只需要监视是否按下了 TAB 键。
So the code would look like something similar where TextBox1 is the one you leave and TB2 is the one you enter;
所以代码看起来很相似,其中 TextBox1 是您离开的,TB2 是您输入的;
Private Sub TextBox2_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 9 Then TextBox1.SetFocus 'where 9 is the KeyCode for the TAB button
End Sub