1. 程式人生 > >C#中按Esc鍵退出Form

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