简体   繁体   English

KeyDown事件键不起作用-VB.net

[英]KeyDown Event Key Not Work - VB.net

KeyDown event does not work, pressing escape the form does not close KeyDown事件不起作用,按转义表单无法关闭

Private Sub DataTable_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
    If e.KeyCode = Keys.Escape Then
        Me.Close()
    End If
End Sub

Well, sure it works, the event is just not very like to fire. 好吧,可以肯定的是,这次活动不是很喜欢开火。 Keystrokes raise the KeyDown event on the control with the focus. 按键引发具有焦点的控件上的KeyDown事件。 That will only ever be your form when it has no controls that can get the focus. 只有当它没有可以获取焦点的控件时,它才会成为您的表单。 A fairly unlikely scenario. 相当不可能的情况。

If you already have a Button labeled "Cancel" that closes the form then set the form's CancelButton property . 如果您已经有一个标记为“取消”的按钮来关闭表单,则设置表单的CancelButton属性

If you don't have such a button then it gets to be pretty unlikely that the user will figure out by himself that the Escape key is useful. 如果您没有这样的按钮,那么用户将不太可能自己发现Escape键是有用的。 He will most likely use the Close button in the upper right corner. 他很可能会使用右上角的“关闭”按钮。 You can nevertheless make it work by overriding the ProcessCmdKey() method. 但是,您可以通过重写ProcessCmdKey()方法来使其工作。 Like this: 像这样:

Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
    If keyData = Keys.Escape Then
        Me.Close()
        Return True
    End If
    Return MyBase.ProcessCmdKey(msg, keyData)
End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM