MessageBox Buttons?

前端 未结 6 2021
太阳男子
太阳男子 2020-12-28 14:14

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

相关标签:
6条回答
  • 2020-12-28 14:50

    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

    0 讨论(0)
  • 2020-12-28 14:52

    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
                }
    
    0 讨论(0)
  • 2020-12-28 14:55

    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
                   }
          }));
       }
    }
    
    0 讨论(0)
  • 2020-12-28 15:06

    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;
        }
    }
    
    0 讨论(0)
  • 2020-12-28 15:06
    if(DialogResult.OK==MessageBox.Show("Do you Agree with me???"))
    {
             //do stuff if yess
    }
    else
    {
             //do stuff if No
    }
    
    0 讨论(0)
  • 2020-12-28 15:07
    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
    }
    
    0 讨论(0)
提交回复
热议问题