Search code examples
c#winformsmessageboxdialogresult

DialogResult MessageBox disable Space key (spacebar) from submitting answer


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
}                        

Solution

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