Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a message box with "Yes", "No" choices and a DialogResult?

I want to make simple Yes/No choiced MessageBox, but I think it is nonsense to design a form for that. I thought I could use MessageBox, add buttons, etc. to accomplish this. It is simple, but since there is no DialogResult returned, how do I retrieve the result?

like image 698
Petr Avatar asked Jun 14 '10 11:06

Petr


People also ask

What is message box in C#?

MessageBox is a class in C# and Show is a method that displays a message in a small window in the center of the Form. MessageBox is used to provide confirmations of a task being done or to provide warnings before a task is done. Create a Windows Forms app in Visual Studio and add a button on it.

What is the message box?

Definition. Displays a message window, also known as a dialog box, which presents a message to the user. It is a modal window, blocking other actions in the application until the user closes it. A MessageBox can contain text, buttons, and symbols that inform and instruct the user.


2 Answers

This should do it:

DialogResult dialogResult = MessageBox.Show("Sure", "Some Title", MessageBoxButtons.YesNo); if(dialogResult == DialogResult.Yes) {     //do something } else if (dialogResult == DialogResult.No) {     //do something else } 
like image 152
Mikael Svenson Avatar answered Sep 28 '22 17:09

Mikael Svenson


DialogResult dr = MessageBox.Show("Are you happy now?",                        "Mood Test", MessageBoxButtons.YesNo); switch(dr) {    case DialogResult.Yes:       break;    case DialogResult.No:       break; } 

MessageBox class is what you are looking for.

like image 38
SwDevMan81 Avatar answered Sep 28 '22 18:09

SwDevMan81