退出按钮来关闭Windows窗体在C#中形成 [英] Escape button to close Windows Forms form in C#
本文介绍了退出按钮来关闭Windows窗体在C#中形成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我曾尝试以下内容:
private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if ((Keys) e.KeyValue == Keys.Escape)
this.Close();
}
不过,这是行不通的。
But it doesn't work.
然后我尝试这样的:
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (e.KeyCode == Keys.Escape)
this.Close();
}
,仍然一无所获的工作。
And still nothing's working.
在我的Windows窗体属性的关键preVIEW设置为true ...什么我做错了?
The KeyPreview on my windows form properties is set to true... What am I doing wrong?
推荐答案
这会一直工作,不管正确的事件处理程序分配,重点preVIEW的,CancelButton等:
This will always work, regardless of proper event handler assignment, KeyPreview, CancelButton etc:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
if (keyData == Keys.Escape) {
this.Close();
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
这篇关于退出按钮来关闭Windows窗体在C#中形成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文