Nagu
Nagu

Reputation: 5114

Esc key in Windows Forms application works only with debugger

Is there any way to handle the Esc key event in Windows Forms?

Actually it is working fine when I put debugger in the code. Once I remove the debugger it is not getting fired.

What's the problem here?

Upvotes: 0

Views: 298

Answers (2)

Savas Adar
Savas Adar

Reputation: 4262

you should add Form.KeyPreview = true in your Form.Designer.cs. And later you can use Form.KeyPress event.

Upvotes: 0

Peter Mortensen
Peter Mortensen

Reputation: 31603

If it is for a dialog then you can set the property CancelButton (of the form) to the button that cancels (e.g. named btnCancel and with text "Cancel" in a English language application.)

In this case you don't need to handle the key event for the Esc key.

Upvotes: 2

Related Questions