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

前端 未结 11 1806
时光取名叫无心
时光取名叫无心 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:05

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

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

    Try this:

    if (MessageBox.Show("Are you sure", "Title_here", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
    {
        Do something here for 'Yes'...
    }
    
    0 讨论(0)
  • 2020-11-28 01:12

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

    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

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

    0 讨论(0)
提交回复
热议问题