How to close a form when pressing the escape key in C Sharp .net

On visual studio C Sharp .net windows form application if you have a form which you want to close on escape key press, you need to have a button (Cancel Button) on the form. If you click on the button the form will close. So that is Close Button with any of below listed code…

this.Hide();
or
Application.Exit();
or
this.Close();
private void btnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

Then on form properties you need to Set the value for CancelButton with the above mentioned button name selected.

Check the below attached graphical example to do it….