Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get the ESC key to close a dialog in Winforms?

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!

like image 649
RobertTheGrey Avatar asked May 15 '09 13:05

RobertTheGrey


People also ask

How do I close dialog Winforms?

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.

How do I close windows with Esc?

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.

Which property is used to exit close the form when pressed Esc?

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.

How do you use the Esc key?

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.


5 Answers

Set the CancelButton property of the form to reference your Cancel button.

like image 142
RichieHindle Avatar answered Oct 20 '22 00:10

RichieHindle


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);
}
like image 27
Adam Butler Avatar answered Oct 19 '22 23:10

Adam Butler


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();
}
like image 36
mahendra apte Avatar answered Oct 19 '22 23:10

mahendra apte


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

like image 32
ChrisF Avatar answered Oct 19 '22 23:10

ChrisF


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.

like image 42
Murph Avatar answered Oct 19 '22 23:10

Murph