views:

1172

answers:

6

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.

+6  A: 

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

This will return DialogResult.Cancel

BC
or MessageBoxButtons.OKCancel
R. Bemrose
Except cancel is not a valid answer to the question "Are you sure you want to..."
Ian Boyd
... and escape is not a valid answer to "Yes or No?"
BC
Escape is not an answer at all, it's a "Oh god, what did i do, i don't know!"
Ian Boyd
+1  A: 

I suspect that if you include MessageBoxButtons.YesNoCancel, by default Escape is wired to cancel which will return a MessageBoxResult of Cancel.

Brian Rudolph
What does it mean if the user hits "No" verses "Cancel"?
Ian Boyd
No and Cancel mean essentially the same thing in this case. YesNoCancel is appropriate for things like "Would you like to save changes to your document before closing HelloWorld?" Yes: Save please. No: Close without saving. Cancel: Don't close or save, just go back.
JMD
+1  A: 

Further to the other answers it is probably bad practice to allow a user to close a dialog with escape unless it shows a "Cancel" button as this differs from the default behaviour that users expect from Windows applications and they may become uncertain as to which action they just took by pressing the escape key.

As already mentioned adding a "Cancel" button will automatically create the desired effect.

jheriko
Escape means, "Oh shit, I didn't mean to do that, pretend I didn't do whatever I did that made this thing appear."
Ian Boyd
+1  A: 

I don't believe this is possible, although the typical equivalent is specifying one of the MessageBoxButtons flags with Cancel (specifically YesNoCancel in your case).

It should be fairly straightforward to recreate your own MessageBox function with this feature, if you really want it, though really Cancel does the job.

Noldorin
Accepted for "not possible"
Ian Boyd
+1  A: 

I think MessageBox (both the one in WinForms and WPF) is one of those components that are best redefined. Its behavior is so rigid that any sort of refactoring is next to impossible. In short, roll your own - that way you can have Escape close it, and anything else you may require.

There's a project on CodePlex called InfoBox that could be of use to you.

Dmitri Nesteruk
+1  A: 

It sounds like MessageBoxButtons.OkCancel is what you are looking for, you are just hung up on the fact that the question does not match Ok/Cancel.

You could in theory write your own MessageBox form that does the same thing and associates No with the Escape key, that would accomplish exactly what you want, with a bit of extra work.

Another option is to rephrase your question, for example:

MessageBox.Show(this, "The following operation will format your computer.\r\nPress Ok to continue, Cancel to abort", "Confirm Format", MessageBoxButtons.OkCancel, MessageBoxIcon.Warning);

Just an example, but it is likely the simplest solution to your dilemma.

Guvante
Usability vs ease of use. Hmmm
Ian Boyd