C# 中的消息框
Muhammad Maisam Abbas
2023年10月12日

本教程将讨论如何在 C# 中创建带有是
或否
选项的消息框。
在 C# 中使用 MessageBox
类创建一个消息框
MessageBox
类在 C# 中显示一个消息窗口。如果要创建带有是
和否
选项的消息框,则可以在 MessageBox
类构造函数的参数中传递 MessageBoxButton.YesNo
枚举。下面的代码示例向我们展示了如何使用 C# 中的 MessageBox
类创建带有是
和否
选项的消息框。
using System.Windows.Forms;
namespace messagbox {
static class Program {
static void Main() {
Application.EnableVisualStyles();
DialogResult dr = MessageBox.Show("Are you happy now?", "Mood Test", MessageBoxButtons.YesNo);
switch (dr) {
case DialogResult.Yes:
MessageBox.Show("That is Fantastic");
break;
case DialogResult.No:
MessageBox.Show("Why Not?");
break;
}
}
}
}
输出:
在上面的代码中,我们将 MessageBox.Show()
函数的结果存储在 DialogResult
枚举的 dr
实例中。DialogResult
枚举包含代表对话框返回值的标识符。我们使用 switch
语句检查 MessageBox.Show()
函数返回的值,并为每个所选选项显示另一个消息框。
Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.
LinkedIn