bjh Hans
bjh Hans

Reputation: 979

how to close app by pressing just Esc. key in C#.Net

How could i make my Win app that when user press Esc. key and the App. going to close

Upvotes: 1

Views: 6471

Answers (2)

Oliver
Oliver

Reputation: 45101

Set Form.KeyPreview = true and add an event handler to Form.KeyDown. Within this event handler add the following code:

if (e.KeyCode == Keys.Escape)
{
    this.Close();
}

Upvotes: 7

Rune Grimstad
Rune Grimstad

Reputation: 36310

Edit: Changed my answer since you changed your question :-)

Add a close button to your form and set the CancelButton property of your form to that button. Add code to the Button.Click event handler that calls the forms Close method.

Now you have a button that closes the form and when the user presses escape that button will be clicked.

Upvotes: 7

Related Questions