vba 如何在表单中的文本框中自动使用文本光标(与选择的命令按钮相反)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17093581/
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
How to Automatically have Text Cursor in Text Box (as Opposed to Command Button Selected) in Form?
提问by Kurt Wagner
I have two Excel picture objects linked to different forms, each with a text box and OK/Cancel buttons. In one form, the text cursor is in the textbox when clicking the object which is what I want:
我有两个链接到不同表单的 Excel 图片对象,每个对象都有一个文本框和确定/取消按钮。在一种形式中,当单击我想要的对象时,文本光标位于文本框中:
but in another it selects the OK command button rather than have the text cursor in the textbox:
但在另一个它选择 OK 命令按钮而不是在文本框中有文本光标:
I've gone through the form and textbox/command button properties and see nothing about selection, and the 'correct' macro properties appear to be the same as the 'incorrect' one.
我已经浏览了表单和文本框/命令按钮属性,但没有看到任何关于选择的内容,“正确”的宏属性似乎与“不正确”的宏属性相同。
What do I do to change the form such that when it's opened the text cursor goes to the textbox instead of the command button being selected?
我该怎么做才能更改表单,以便在打开时文本光标转到文本框而不是选择的命令按钮?
回答by Kazimierz Jawor
Quite easy solution is to change TabIndex Property
to 0.
非常简单的解决方案是更改TabIndex Property
为 0。
So, 1)go to VBA Editor, 2)select your TextBox in your UserForm and 3)change TabIndex
Property in Property window as presented below.
因此,1)转到 VBA 编辑器,2)在您的用户窗体中选择您的文本框,3)TabIndex
在属性窗口中更改 属性,如下所示。
回答by Ripster
Add an event to the form so that when it is initialized it selects the correct texbox.
向表单添加一个事件,以便在初始化时选择正确的 texbox。
Private Sub UserForm_Initialize()
TextBox2.SetFocus
End Sub
回答by David Zemens
Before the form is displayed, you can do soemthing like:
在显示表单之前,您可以执行以下操作:
TextBox1.SetFocus
TextBox1.SetFocus
Obviously, replace "TextBox1" with whatever the name of your textbox object is.
显然,用您的文本框对象的名称替换“TextBox1”。
This should go in whatever event or macro causes the form to .Show
, immediately before the .Show
statement.
这应该出现在任何事件或宏导致表单.Show
, 紧接在.Show
语句之前。