且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检测不同控件中的按键

更新时间:2022-04-28 08:51:41

在第一段代码中,您覆盖了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!