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

前端 未结 11 1807
时光取名叫无心
时光取名叫无心 2020-11-28 00:40

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

相关标签:
11条回答
  • 2020-11-28 01:18
    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.

    0 讨论(0)
  • 2020-11-28 01:18

    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...

    0 讨论(0)
  • 2020-11-28 01:18
    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

    0 讨论(0)
  • 2020-11-28 01:21
    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
    }
    
    0 讨论(0)
  • 2020-11-28 01:26

    @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);
    
    0 讨论(0)
提交回复
热议问题