我想做一个简单的Yes/No选择的MessageBox,但是我认为为它设计表单是毫无意义的。我认为我可以使用MessageBox,添加按钮等来完成这一点。这很简单,但由于没有返回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'
其他回答
MessageBox.Show(title, text, messageboxbuttons.yes/no)
这将返回一个您可以检查的dialgresult。
例如,
if(MessageBox.Show("","",MessageBoxButtons.YesNo) == DialogResult.Yes)
{
//do something
}
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…
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
}
从这里查看更多细节
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
}
DialogResult dr = MessageBox.Show("Are you happy now?",
"Mood Test", MessageBoxButtons.YesNo);
switch(dr)
{
case DialogResult.Yes:
break;
case DialogResult.No:
break;
}
MessageBox类就是您要找的。