26

How would I say if the yes button on the messagebox was pressed do this,that and the other? In C#.

6TTW014
  • 607
  • 2
  • 11
  • 22

6 Answers6

68
  1. Your call to MessageBox.Show needs to pass MessageBoxButtons.YesNo to get the Yes/No buttons instead of the OK button.

  2. Compare the result of that call (which will block execution until the dialog returns) to DialogResult.Yes....

if (MessageBox.Show("Are you sure?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
    // user clicked yes
}
else
{
    // user clicked no
}
Unihedron
  • 10,251
  • 13
  • 53
  • 66
Lynn Crumbling
  • 12,098
  • 7
  • 56
  • 91
  • 1
    @xxMUROxx Please do not edit answers to refactor code. If you feel the need to add additional content with improvements, either leave a comment or add your own answer. – Lynn Crumbling May 11 '15 at 13:15
11

If you actually want Yes and No buttons (and assuming WinForms):

void button_Click(object sender, EventArgs e)
{
    var message = "Yes or No?";
    var title = "Hey!";
    var result = MessageBox.Show(
        message,                  // the message to show
        title,                    // the title for the dialog box
        MessageBoxButtons.YesNo,  // show two buttons: Yes and No
        MessageBoxIcon.Question); // show a question mark icon

    // the following can be handled as if/else statements as well
    switch (result)
    {
    case DialogResult.Yes:   // Yes button pressed
        MessageBox.Show("You pressed Yes!");
        break;
    case DialogResult.No:    // No button pressed
        MessageBox.Show("You pressed No!");
        break;
    default:                 // Neither Yes nor No pressed (just in case)
        MessageBox.Show("What did you press?");
        break;
    }
}
Jeff Mercado
  • 113,921
  • 25
  • 227
  • 248
6
if(DialogResult.OK==MessageBox.Show("Do you Agree with me???"))
{
         //do stuff if yess
}
else
{
         //do stuff if No
}
Javed Akram
  • 14,220
  • 23
  • 77
  • 113
  • 2
    Thank you very much kind sir :) – 6TTW014 Mar 24 '11 at 03:08
  • 4
    This won't work as espected, 'cause the sentences in the else won't be called anytime. – james_bond Mar 24 '11 at 17:24
  • @james_bond: Close the dialog and check for else part. – Javed Akram Mar 25 '11 at 02:15
  • 2
    If you're not using one of the `Show` overloads containing the `MessageBoxButtons` parameter, the `MessageBox` will not have any button other than Ok, so the `else` part will never be triggered. Even if you close it via the close box in the top right corner, the result is `DialogResult.OK`. – takrl Apr 18 '16 at 10:30
2

An updated version of the correct answer for .NET 4.5 would be.

if (MessageBox.Show("Are you sure?", "Confirm", MessageBoxImage.Question) 
    == MessageBoxResult.Yes)  
{
// If yes
}
else  
{
// If no
}

Additionally, if you wanted to bind the button to a command in a view model you could use the following. This is compatible with MvvmLite:

public RelayCommand ShowPopUpCommand
{
   get
   {
   return _showPopUpCommand ??
      (_showPopUpCommand = new RelayCommand(
         () =>
               {
                // Put if statement here
               }
      }));
   }
}
piofusco
  • 229
  • 2
  • 14
0

Check this:

                     if (
 MessageBox.Show(@"Are you Alright?", @"My Message Box",MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        //YES ---> Ok IM ALRIGHHT
                    }
                    else
                   {
                   //NO --->NO IM STUCK
                    }

Regards

Crimsonland
  • 2,134
  • 3
  • 24
  • 40
-1

This way to check the condition while pressing 'YES' or 'NO' buttons in MessageBox window.

DialogResult d = MessageBox.Show("Are you sure ?", "Remove Panel", MessageBoxButtons.YesNo);
            if (d == DialogResult.Yes)
            {
                //Contents
            }
            else if (d == DialogResult.No)
            {
                //Contents
            }
Ismayil S
  • 190
  • 2
  • 18