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


当前回答

试试这个:

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

其他回答

试试这个:

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

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…

这个简单的代码对我很有用。我从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'
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
}

MessageBox确实会产生一个dialgresults

DialogResult r = MessageBox.Show("Some question here");

您还可以很容易地指定按钮。更多文档可以在http://msdn.microsoft.com/en-us/library/ba2a6d06.aspx上找到