How Do I Trigger An Event on the press of a key in VB?

感情迁移 提交于 2020-05-16 20:05:47

问题


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.

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?

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.


回答1:


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


来源:https://stackoverflow.com/questions/23176009/how-do-i-trigger-an-event-on-the-press-of-a-key-in-vb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!