编辑:自从您更改了问题后更改了我的答案:-)
在表单中添加一个关闭按钮,并将表单的CancelButton属性设置为该按钮.将代码添加到Button.Click调用forms Close方法的事件处理程序中.
现在您有一个关闭表单的按钮,当用户按下escape时,将单击该按钮.
设置Form.KeyPreview = true并添加事件处理程序Form.KeyDown.在此事件处理程序中添加以下代码:
if (e.KeyCode == Keys.Escape)
{
this.Close();
}
Run Code Online (Sandbox Code Playgroud)