.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Windows Form Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Close form with press of Escape button

Posted By:Ravi Ranjan Kumar       Posted Date: June 29, 2011    Points: 40    Category: Windows Form Controls    URL: http://www.dotnetspark.com  

This Code snippet enable you to learn how to close Form after pressing of Escape button.
 

Simply add given code in your program 

protected override bool ProcessCmdKey(ref Message msg, Keys keyData) 
{
     if (keyData == Keys.Escape) this.Close();
            bool res = base.ProcessCmdKey(ref msg, keyData);
            return res;
}

or alternate way write below code at KeyPress event of form 
 if (e.KeyChar == (char)27)
                this.Close();
after using this code when you press Escape button, the program automatically close itself.



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend