我想做一个简单的Yes/No选择的MessageBox,但是我认为为它设计表单是毫无意义的。我认为我可以使用MessageBox,添加按钮等来完成这一点。这很简单,但由于没有返回dialgresult,我如何检索结果?


当前回答

@Mikael Svenson的答案是正确的。我只是想补充一点:

消息框图标还可以包含一个额外的属性,如下所示:

DialogResult dialogResult = MessageBox.Show("Sure", "Please Confirm Your Action", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

其他回答

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
}

试试这个:

if (MessageBox.Show("Are you sure", "Title_here", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
    Do something here for 'Yes'...
}

这个简单的代码对我很有用。我从MSDN上下载的:

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'
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
}

从这里查看更多细节

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
}

在Windows Phone上使用MessageBoxResult而不是dialgresult…