如何按VB中的一个键触发事件? [英] How Do I Trigger An Event on the press of a key in VB?

查看:47
本文介绍了如何按VB中的一个键触发事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Visual Basic中有一个应用程序,当在文本框中输入用户整数时,会显示一个消息框,其文本范围为A-F.

I have an app in visual basic that displays a message box with a text range from A - F upon user integer input in a textbox.

我在表单上有一个按钮控件来触发事件,但是用户每次必须按下按钮才能得到结果,这很酷,但是如果我想通过说 B 我键盘上的键?

I have a button control on the form that triggers the event, but the user has to press the button every time to get a result, that's cool, but what if I wanted the event triggered by say the B key on my keyboard?

我知道在Visual Studio 2012中为我的按钮分配 AcceptButton 属性的方法,该属性仅接受 Enter 键.我想要的效果与按下键盘上的某个键时所按下的按钮相同.谢谢.

I know of a way of assigning the AcceptButton property to my button in visual studio 2012 which accepts just the Enter key alone. I want the same effect as pressing the button on the press of a key on a keyboard instead. Thanks.

推荐答案

使用窗体Keydown事件,并检查是否按下了B键.如果是这样,请显示消息框.

Use the form Keydown event, and check to see if the B key was pressed. If so, display the messagebox.

Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.B Then MsgBox("B key was pressed")
End Sub

这篇关于如何按VB中的一个键触发事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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