DialogResult MessageBox disable Space key (spacebar) from submitting answer

555 views Asked by At

I use the code below to create a MessageBox with dialogue. By default, the yes button has focus. If I press the Space key accidentally, the dialog understands that I selected Yes. I want that only if I press the return key to trigger the event. Is there a way to disable the Space key from submitting the answer?

DialogResult dialogResult = MessageBox.Show(sMsg, "Title", MessageBoxButtons.YesNo);
if(dialogResult == DialogResult.Yes)
{
   // do something
}
else if (dialogResult == DialogResult.No)
{
  // do something else
}                        
2

There are 2 answers

0
Nick_F On BEST ANSWER

Visual Vincent clarified that it is better to create my own dialog form, instead of relying on MessageBox.Show().

3
David Cardinale On

Try this:

MessageBox.Show(this, "MessageText", "MessageCaption", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No, MessageBoxOptions.None);

the "MessageBoxResult.No" parameter is the default dialogue result value.