我正在尝试让用户确认他们是否想要使用MessageBox删除产品并捕获其结果。这是我的代码:
// Confirm if the user really wants to delete the product
DialogResult result = MessageBox.Show("Do you really want to delete the product \"" + productName + "\"?", "Confirm product deletion", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (result == DialogResult.OK)
{
MessageBox.Show("deleted");
}当我运行代码并尝试删除产品时,deleted永远不会显示。在MSDN页面上显示使用MessageBoxResult而不是DialogResult,但Visual Studio无法识别MessageBoxResult,因此我在代码中的其他地方使用DialogResult打开文件对话框。显然,这不是检查它的正确方法。
发布于 2012-03-28 05:25:46
您必须请求DialogResult.Yes
// Confirm if the user really wants to delete the product
DialogResult result = MessageBox.Show("Do you really want to delete the product \"" + productName + "\"?", "Confirm product deletion", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (result == DialogResult.Yes)
{
MessageBox.Show("deleted");
} 发布于 2012-03-28 05:26:08
您已将消息框类型设置为yes/no,但您仍试图捕获OK结果。回答“是”,你就会被排序。
发布于 2012-03-28 05:27:30
您使用的是YesNo按钮,因此DialogResult.OK与此无关。你应该这样做
if (result == DialogResult.Yes)为了你的健康状况。
https://stackoverflow.com/questions/9897920
复制相似问题