整个表单上的vb.net Keydown事件 [英] vb.net Keydown event on whole form
问题描述
我有一个带有多个控件的表单.无论任何控件事件如何,我都希望在keydown事件上运行特定的子项. 我的意思是,如果用户在窗体上的任意位置按下Ctrl + S,它将执行一个子例程.
I have a form with several controls. I want to run a specific sub on keydown event regardless any controls event. I mean if user press Ctrl+S anywhere on form it execute a subroutine.
推荐答案
You should set the KeyPreview property on the form to True and handle the keydown event there
将此属性设置为true时,表单将接收所有KeyPress, KeyDown和KeyUp事件.表单的事件处理程序之后 完成击键处理后,将击键分配给 有重点的控制. ..........处理键盘 事件仅在表单级别,并且不允许控件接收 键盘事件,请在您的键盘上设置KeyPressEventArgs.Handled属性 窗体的KeyPress事件处理程序为true.
When this property is set to true, the form will receive all KeyPress, KeyDown, and KeyUp events. After the form's event handlers have completed processing the keystroke, the keystroke is then assigned to the control with focus. .......... To handle keyboard events only at the form level and not allow controls to receive keyboard events, set the KeyPressEventArgs.Handled property in your form's KeyPress event handler to true.
例如,要处理Control + S组合键,您可以为KeyDown事件表单编写此事件处理程序.
So, for example, to handle the Control+S key combination you could write this event handler for the form KeyDown event.
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles MyBase.KeyDown
If e.Control AndAlso e.KeyCode = Keys.S then
' Call your sub method here .....
YourSubToCall()
' then prevent the key to reach the current control
e.Handled = False
End If
End Sub
这篇关于整个表单上的vb.net Keydown事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!