1.在Load中设置keypreview设定为true
2.在KeyPress事件中定义关闭事件
//Load事件
private void Form1_Load(object sender, EventArgs e)
{
this.KeyPreview = true;
}
//KeyPress事件
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Escape)
{
this.Close();
}
}
本文介绍了一种在C#中使用ESC键关闭Form的方法。通过在Load事件中设置KeyPreview属性为true,并在KeyPress事件中检测ESC键,实现Form的快捷关闭功能。
4261

被折叠的 条评论
为什么被折叠?



