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

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

提交回复
热议问题