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….

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: