更新时间:2022-04-28 08:51:41
在第一段代码中,您覆盖了OnKeyDown
.当您是派生类并且在发生此事件时需要自己做些事情时,才完成此操作.
在第二段代码中,使用EditBox_KeyDown
处理事件.您需要确保为每个控件实现事件处理程序.
如果要获取整个表单的keydown事件(无论关注什么控件),请将Form
的KeyPreview
设置为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 overrideOnKeyDown
. 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 theEditBox_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 theKeyPreview
of theForm
toTrue
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!