我想创建一个简单的确认对话框,内容为“请确认信息是否正确,如果确定无误,请点击‘确定’”。是否有这样的内置功能?
这是一个示例。你可以尝试类似这样的东西。
var confirmResult = MessageBox.Show("Are you sure to delete this item ??",
"Confirm Delete!!",
MessageBoxButtons.YesNo);
if (confirmResult == DialogResult.Yes)
{
// If 'Yes', do something here.
}
else
{
// If 'No', do something here.
}
你也可以尝试使用MessageBoxButtons.OKCancel
代替MessageBoxButtons.YesNo
,这取决于你的要求。
MessageBoxResult confirmResult = MessageBox.Show("Are you sure to delete this item ??", "Confirm Delete!!", MessageBoxButton.YesNo);`
if (confirmResult == MessageBoxResult.Yes)
{
// If 'Yes', do something here.
}
else
{
// If 'No', do something here.
}
MessageBox.Show
是一个可以指定标题、提示消息和显示的按钮选项的方法。
另一方面,如果您需要让用户确认信息,那么您可能需要显示自定义对话框,您可以使用 Form.ShowDialog
方法实现。
在 .Net Core
中,您可以这样做:
DialogResult dialogResult= MessageBox.Show("Are you sure to delete?", "Confirm", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
//if code here....
}
else
{
//else code here....
}