C# MessageBox dialog result

biox picture biox · May 27, 2012 · Viewed 253.6k times · Source

I want to make a MessageBox confirmation. Here is the message box:

MessageBox.Show("Do you want to save changes?", "Confirmation", messageBoxButtons.YesNoCancel);

And I want to make something like this (in pseudocode):

if (MessageBox.Result == DialogResult.Yes)
    ;
else if (MessageBox.Result == DialogResult.No)
    ;
else
    ;

How can I do that in C#?

Answer

david.s picture david.s · May 27, 2012
DialogResult result = MessageBox.Show("Do you want to save changes?", "Confirmation", MessageBoxButtons.YesNoCancel);
if(result == DialogResult.Yes)
{ 
    //...
}
else if (result == DialogResult.No)
{ 
    //...
}
else
{
    //...
}