Friday, April 16, 2010

On Esc close the window

This concept always fascinated me. I always wondered how to do it in .NET. Then came the MSDN in rescue. So here I am sharing a tip to do it.

Step 1 : In the windows form there is a property KeyPreview make it to True . By default it is False.


Step 2 : In the KeyDown event of the form write this small code.

if (e.KeyCode == Keys.Escape)
this.Dispose();


4 comments:

Anonymous said...

nice

mahendra said...

another way is make a hidden button
and set form's cancelbutton property
to hidden button.
Write the code for hidden button.
this.close();

Ketan - www.ketans.com said...
This comment has been removed by the author.
Ketan said...

@mahendra Kool idea