Often when using software these days, the ESC key will close a dialog without persisting any changes I've made. I like that especially because even though the dialog may have a cancel button on it, I don't necessarily want to reach for the mouse or tab over to the cancel button. It's a nice clean way of saying "Oops, didn't meant to do that!" to the software. In fact, I find I get annoyed with software that doesn't implement this feature and I can't believe that it's not already done under the hood of Winforms - seems intuitive to me...
I have looked at hooking into the Form KeyPress event, or trying to simulate a "Cancel" DialogResult etc. There seem to be a number of ways of going about this...
So the question is: What is the cleanest way of going about binding the ESC key to close the dialog without saving any changes?
Any help from those that have experience in this is much appreciated!
To close a form, you just need to set the form's DialogResult property (to any value by DialogResult. None ) in some event handler. When your code exits from the event handler the WinForm engine will hide the form and the code that follows the initial ShowDialog method call will continue execution.
Some windows can be closed by pressing the esc key, like the Preference window in any app or the 'Safari help' window or the 'Fonts' window in TextExit for example. Other windows can not be closed by pressing the esc key, like the Safari browser window or the 'Special Characters' window.
Set the CancelButton property of the form to that button. Gets or sets the button control that is clicked when the user presses the Esc key. You will also have to set the KeyPreview property to true.
True to its name, the Esc key acts like a “Cancel” button on most computers. On Windows, for example, the key can be used to close a dialog box without having to click “Cancel” with a mouse button. You can also use the Esc key to cancel loading a website in a browser.
Set the CancelButton
property of the form to reference your Cancel button.
To do this when you don't have a cancel button you can override processcmdkey:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Escape)
{
this.Close();
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
You can do this:
button b = new button();
b.click = button_click;
this.cancelbutton = b;
protected void button_click(object sender,eventargs e)
{
this.close();
}
You can also set the CancelButton property of the form to the cancel button:
this.CancelButton = this.cancelButton;
In Visual Studio you can set this via the Properties of the form and the code is added to the Form.Designer.cs file
To a certain extent it depends on your development environment - in .NET its built in (as it was in vb.old), forms has a "Cancel Button" property "If this property is set, the button is 'clicked' whenever the user presses the 'ESC' key."
There is a corresponding "AcceptButton" property for the 'ENTER' key.
From memory, standard message and dialog boxes will follow the above logic by default - though of course one can specify the default button for a message box where you don't want stuff to happen without the user positively asserting that that's what they want to do.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With