Answers:
应该这样做:
DialogResult dialogResult = MessageBox.Show("Sure", "Some Title", MessageBoxButtons.YesNo);
if(dialogResult == DialogResult.Yes)
{
//do something
}
else if (dialogResult == DialogResult.No)
{
//do something else
}
DialogResult dr = MessageBox.Show("Are you happy now?",
"Mood Test", MessageBoxButtons.YesNo);
switch(dr)
{
case DialogResult.Yes:
break;
case DialogResult.No:
break;
}
您正在寻找MessageBox类。
MessageBox 确实会产生一个DialogResults
DialogResult r = MessageBox.Show("Some question here");
您也可以轻松地指定按钮。可以在http://msdn.microsoft.com/zh-cn/library/ba2a6d06.aspx中找到更多文档。
采用:
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代替DialogResult ...
您还可以将此变体与文本字符串一起使用,这是在C#2012中测试的完整更改的代码(来自Mikael的代码):
// 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
}
你可以之后
.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
}
这个简单的代码为我工作。我在这里从MSDN上获取了它:
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'