退出时双重确认
我试图这样做,以便提示用户确认退出我的 C# 程序,但由于某种原因,如果他们说“是”他们想退出,确认框会再次弹出。我不明白为什么。
if (MessageBox.Show("Are you sure you want to exit?", "Confirm exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
e.Cancel = true;
}
else { Application.Exit(); }
I am trying to make it so that the user is prompted to confirm exiting my program in c#, but for some reason, if they say "yes" they would like to exit, the confirmation box would pop up again. I can't figure out why.
if (MessageBox.Show("Are you sure you want to exit?", "Confirm exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
e.Cancel = true;
}
else { Application.Exit(); }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
用这个
Use this
啊,您检查了
FormClosing
事件的CloseReason
了吗?我认为您可能会因为两个不同的原因而遇到相同的事件(尽管我并不完全期望这种情况会正常发生);检查您的FormClosingEventArgs
以查看参数是什么。Ah, did you check the
CloseReason
for theFormClosing
event? I think you might get the same event for two different reasons (although I don't exactly expect that to happen normally); check yourFormClosingEventArgs
to see what the parameters are.啊,我想出了如何解决它。我删除了 Application.Exit();事件从 FormClosing 事件中转移到 FormClosed 事件中。现在一切正常了。
Ah, I figured out how to fix it. I removed the Application.Exit(); event from the FormClosing event, and moved it into the FormClosed event. It all works now.
作为 SFD,他说您需要使用消息框创建事件。我已经添加了过滤器,如果用户正在关闭表单和警告消息框:
您需要将事件分配给表单以使其工作:
如果您想让它停止,以便用户可以再次关闭窗口而不显示消息:
As SFD he said you need to create the event with the message box. I've added to filter if the user it's closing the form and a warning messagebox:
You nee to assign the event to the form to make it work:
If you want to make it stop so the user can close again the window without the message: