Trying to program code to Exit application upon ESC in C#?
I am learning more C# programming and have developed an application using Ogre. However the only method I have of exiting the application when running it is, ALT+F4.
I have put in the code a method to Exit with keyPress ESC. However, Visual C# Express is throwing error, and not letting me use ProcessCmdKey. :\
Help?
using System;
using System.Windows.Forms;
protected override bool ProcessCmdKey(ref LogMessageLevel msg, Keys keyData)
{
if开发者_开发技巧 (keyData == Keys.Escape) this.Close();
return base.ProcessCmdKey(ref msg, keyData);
}
More standard way is adding close button to the form then set this button as the Form Cancel button.
For more details: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.cancelbutton.aspx
By setting it as Cancel button, it will be triggered automatically upon pressing ESC key.
Try this code :
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Escape)
{
MessageBox.Show("esc pressed!");
this.Close();
}
return base.ProcessCmdKey(ref msg, keyData);
}
精彩评论