EnterKey 在 VBA 用户窗体中按下按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19125240/
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-08 16:47:25  来源:igfitidea点击:

EnterKey to press button in VBA Userform

excelvba

提问by user2385809

I have a userform in Excel that asks for a username and password. Once you enter your password if you press Enterit just "selects" the next item which is the LogInbutton, but it doesn't press it. You have to hit Enteragain to actually press the button.

我在 Excel 中有一个用户表单,要求输入用户名和密码。一旦你输入密码,如果你按下Enter它只是“选择”下一个项目,即LogIn按钮,但它不会按下它。你必须Enter再次点击才能真正按下按钮。

How can I make it so when the user presses enter on his keyboard the LogIn button is pressed and the code associated to is runs (Logincode_click)?

当用户在他的键盘上按下 Enter 键时,我如何才能做到这一点,登录按钮被按下并且与之关联的代码运行(Logincode_click)?

回答by Jacob D

You could also use the TextBox's On Key Press event handler:

您还可以使用 TextBox 的 On Key Press 事件处理程序:

'Keycode for "Enter" is 13
Private Sub TextBox1_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = 13 Then
         Logincode_Click
    End If
End Sub

Textbox1 is an example. Make sure you choose the textbox you want to refer to and also Logincode_Click is an example sub which you call (run) with this code. Make sure you refer to your preferred sub

Textbox1 就是一个例子。确保您选择要引用的文本框,并且 Logincode_Click 是您使用此代码调用(运行)的示例子。确保您参考您的首选子

回答by drognisep

Be sure to avoid "magic numbers" whenever possible, either by defining your own constants, or by using the built-in vbXXXconstants.

通过定义自己的常量或使用内置的vbXXX常量,确保尽可能避免“幻数” 。

In this instance we could use vbKeyReturnto indicate the enter key's keycode (replacing YourInputControland SubToBeCalled).

在这种情况下,我们可以使用vbKeyReturn来指示输入键的键码(替换YourInputControlSubToBeCalled)。

   Private Sub YourInputControl_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
        If KeyCode = vbKeyReturn Then
             SubToBeCalled
        End If
   End Sub

This prevents a whole category of compatibility issues and simple typos, especially because VBA capitalizes identifiers for us.

这可以防止一整类兼容性问题和简单的拼写错误,尤其是因为 VBA 为我们将标识符大写。

Cheers!

干杯!

回答by Pepys

This one worked for me

这个对我有用

Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
        If KeyCode = 13 Then
             Button1_Click
        End If
End Sub

回答by Greedo

Further to @Penn's comment, and in case the link breaks, you can also achieve this by setting the Defaultproperty of the button to True(you can set this in the properties window, open by hitting F4)

进一步@Penn的评论,万一链接断开,您也可以通过将Default按钮的属性设置为True(您可以在属性窗口中设置,点击打开F4

That way whenever Returnis hit, VBA knows to activate the button's click event. Similarly setting the Cancelproperty of a button to Truewould cause that button's click event to run whenever ESCkey is hit (useful for gracefully exiting the Userform)

这样,无论何时Return点击,VBA 都知道激活按钮的点击事件。类似地Cancel,将按钮的属性设置为True将导致该按钮的单击事件在ESC击中键时运行(对于优雅地退出用户窗体很有用)



Source: Olivier Jacot-Descombes's answer accessible here https://stackoverflow.com/a/22793040/6609896

资料来源:Olivier Jacot-Descombes 的回答,可在此处访问https://stackoverflow.com/a/22793040/6609896

回答by David Zemens

Use the TextBox's Exitevent handler:

使用 TextBox 的Exit事件处理程序:

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Logincode_Click  
End Sub

回答by Sashidhar A

Here you can simply use:

在这里您可以简单地使用:

SendKeys "{ENTER}"at the end of code linked to the Username field.

SendKeys "{ENTER}"在链接到用户名字段的代码末尾。

And so you can skip pressing ENTER Key once (one time).
And as a result, the next button ("Log In" button here) will be activated. And when you press ENTER once (your desired outcome), It will run code which is linked with "Log In" button.

因此您可以跳过按一次 ENTER 键(一次)。
结果,下一个按钮(此处为“登录”按钮)将被激活。当您按一次 ENTER(您想要的结果)时,它将运行与“登录”按钮链接的代码。