C#中按Esc键退出Form

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

猜你喜欢

转载自blog.csdn.net/Tiger_shl/article/details/83114433