Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET: How to have Escape close a MessageBox.Show()?

Assuming the code:

MessageBox.Show(this, 
       "Would you like the differential girdle spring on the up-end of the grammeters?", 
       "Smi-Boloid Stater Slots", 
       MessageBoxButtons.YesNo, 
       MessageBoxIcon.Warning);

alt text

If the user presses Escape, i want it to close the message box, and the Show() method can return DialogResult.None.

Related question: How can i enable the "X" in the upper-right of a MessageBox.Show to let the user abort?


Kind of like how a Windows Explorer confirmation dialog lets you press escape to abort.

Kind of like how an Internet Explorer confirmation dialog lets you press escape to abort.

Kind of like how an Outlook confirmation dialog lets you press escape to abort.

Kind of like how every dialog in the history of the universe lets the user press escape ,or click X, in order to say:

"I have no earthly idea what you're asking me, please just don't break my computer."


From the Vista UX Guidelines

Use Yes and No buttons only to respond to yes or no questions. The main instruction should be naturally expressed as a yes or no question. Never use OK and Cancel for yes or no questions.

like image 476
Ian Boyd Avatar asked Feb 12 '09 18:02

Ian Boyd


1 Answers

You have to enable the "Cancel" option with the enum value "MessageBoxButtons.YesNoCancel"

This will return DialogResult.Cancel

enter image description here

like image 140
BC. Avatar answered Nov 03 '22 00:11

BC.