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

11

427

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?

Postal answered 14/6, 2010 at 11:30 Comment(0)
O
937

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
}
Ozmo answered 14/6, 2010 at 11:37 Comment(3)
The answer was for Windows, not Windows Phone which I don't know much about :)Ozmo
'DialogResult' does not work in wpf. You have to use 'MessageBoxResult'.Information
@jean Docs has MessageBoxButtons learn.microsoft.com/en-us/dotnet/api/…Ozmo
K
69
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.

Kamp answered 14/6, 2010 at 11:34 Comment(0)
T
40
MessageBox.Show(title, text, messageboxbuttons.yes/no)

This returns a DialogResult which you can check.

For example,

if(MessageBox.Show("","",MessageBoxButtons.YesNo) == DialogResult.Yes)
{
   //do something
}
Tortile answered 14/6, 2010 at 11:34 Comment(0)
T
18

The MessageBox does produce a DialogResults

DialogResult r = MessageBox.Show("Some question here");

You can also specify the buttons easily enough. More documentation can be found at http://msdn.microsoft.com/en-us/library/ba2a6d06.aspx

Tacnaarica answered 14/6, 2010 at 11:34 Comment(0)
T
14

Use:

MessageBoxResult m = MessageBox.Show("The file will be saved here.", "File Save", MessageBoxButton.OKCancel);
if(m == m.Yes)
{
    // Do something
}
else if (m == m.No)
{
    // Do something else
}

MessageBoxResult is used on Windows Phone instead of DialogResult...

Torn answered 6/8, 2013 at 7:46 Comment(0)
U
9

You can also use this variant with text strings, here's the complete changed code (Code from Mikael), tested in C# 2012:

// Variable
string MessageBoxTitle = "Some Title";
string MessageBoxContent = "Sure";

DialogResult dialogResult = MessageBox.Show(MessageBoxContent, MessageBoxTitle, MessageBoxButtons.YesNo);
if(dialogResult == DialogResult.Yes)
{
    //do something
}
else if (dialogResult == DialogResult.No)
{
    //do something else
}

You can after

.YesNo

insert a message icon

, MessageBoxIcon.Question
Unbearable answered 26/5, 2013 at 20:0 Comment(1)
This doesn't work in 2022Circumlocution
H
7

@Mikael Svenson's answer is correct. I just wanted to add a small addition to it:

The Messagebox icon can also be included has an additional property like below:

DialogResult dialogResult = MessageBox.Show("Sure", "Please Confirm Your Action", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
Hitherto answered 15/12, 2015 at 10:52 Comment(0)
O
7
if (MessageBox.Show("Please confirm before proceed" + "\n" + "Do you want to Continue ?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)

{
//do something if YES
}

else

{
//do something if NO
}

It will Prompt a message box like this.

Odds answered 3/10, 2019 at 5:55 Comment(0)
I
7

Try this:

if (MessageBox.Show("Are you sure", "Title_here", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
    Do something here for 'Yes'...
}
Inshrine answered 11/2, 2020 at 17:52 Comment(0)
H
2

This simple code worked for me. I grabbed it from MSDN here:

https://social.msdn.microsoft.com/Forums/en-US/d1092a96-96b0-4ca4-b716-0c8e55e42ee9/how-can-i-manage-messagebox-result-?forum=Vsexpressvcs

if (System.Windows.Forms.MessageBox.Show
            ("Are you sure you want to add the audit?", "Add",
            System.Windows.Forms.MessageBoxButtons.YesNo, 
            System.Windows.Forms.MessageBoxIcon.Question)
            ==System.Windows.Forms.DialogResult.Yes)                
        // Do stuff after 'YES is clicked'
        else
        // DO stuff after 'NO is clicked'
Halpin answered 24/10, 2014 at 14:54 Comment(0)
M
1
dynamic MsgResult = this.ShowMessageBox("Do you want to cancel all pending changes ?", "Cancel Changes", MessageBoxOption.YesNo);

if (MsgResult == System.Windows.MessageBoxResult.Yes)
{
    enter code here
}
else 
{
    enter code here
}

Check more detail from here

Motive answered 15/6, 2014 at 19:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.