Reputation: 256901
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);
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."
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.
Upvotes: 18
Views: 8818
Reputation: 24938
You have to enable the "Cancel" option with the enum value "MessageBoxButtons.YesNoCancel"
This will return DialogResult.Cancel
Upvotes: 12
Reputation: 147401
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.
Upvotes: 4
Reputation: 19213
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.
Upvotes: 1
Reputation: 23798
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.
Upvotes: 1
Reputation: 3113
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.
Upvotes: 2
Reputation: 6318
I suspect that if you include MessageBoxButtons.YesNoCancel, by default Escape is wired to cancel which will return a MessageBoxResult of Cancel.
Upvotes: 1