检测不同控件中的按键 [英] Detecting a key press in different controls

查看:60
本文介绍了检测不同控件中的按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个控件,要在其中检测按键按下.
起初我用

I have 2 controls in which I want to detect a key pressed.
At first I used

Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)
...
MyBase.OnKeyDown(e)
End Sub


但是我发现按键事件是在错误的控件中引发的,所以我使用了


but I discovered that the key pressed event was raised in the wrong control so I used

Private Sub EditBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown

End Sub


认为这将重点关注keydown事件,但事实并非如此

帮助??


Thinking this would focus the keydown event, but it doesn''t

Help??

推荐答案

在第一段代码中,您覆盖了OnKeyDown.当您是派生类并且在发生此事件时需要自己做些事情时,才完成此操作.

在第二段代码中,使用EditBox_KeyDown处理事件.您需要确保为每个控件实现事件处理程序.

如果要获取整个表单的keydown事件(无论关注什么控件),请将FormKeyPreview设置为True并为表单keydown事件创建一个处理程序.在这里查看更多信息:
http://msdn.microsoft. com/en-us/library/system.windows.forms.form.keypreview%28v = vs.71%29.aspx [
In the first piece of code you override OnKeyDown. This is done when you are a derived class and need to do something yourself when this event occurs.

In the second piece of code you handle the event with the EditBox_KeyDown. You need to make sure that you implement the event handler for each control.

If you want to get the keydown event for the overall form (no matter what control is focused), you set the KeyPreview of the Form to True and create a handler for the form keydown event. Have a look here for more info:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.keypreview%28v=vs.71%29.aspx[^]

Good luck!


这篇关于检测不同控件中的按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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