退出时双重确认

发布于 2024-10-10 23:02:01 字数 312 浏览 3 评论 0原文

我试图这样做,以便提示用户确认退出我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

遗失的美好 2024-10-17 23:02:01

用这个

 private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (MessageBox.Show("Are you sure you want to close?", "Infomate", MessageBoxButtons.YesNo) == DialogResult.No)
        {
            e.Cancel = true;
        }        
    }

Use this

 private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (MessageBox.Show("Are you sure you want to close?", "Infomate", MessageBoxButtons.YesNo) == DialogResult.No)
        {
            e.Cancel = true;
        }        
    }
耳根太软 2024-10-17 23:02:01

啊,您检查了 FormClosing 事件的 CloseReason 了吗?我认为您可能会因为两个不同的原因而遇到相同的事件(尽管我并不完全期望这种情况会正常发生);检查您的 FormClosingEventArgs 以查看参数是什么。

Ah, did you check the CloseReason for the FormClosing event? I think you might get the same event for two different reasons (although I don't exactly expect that to happen normally); check your FormClosingEventArgs to see what the parameters are.

[浮城] 2024-10-17 23:02:01

啊,我想出了如何解决它。我删除了 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.

勿忘初心 2024-10-17 23:02:01

作为 SFD,他说您需要使用消息框创建事件。我已经添加了过滤器,如果用户正在关闭表单和警告消息框:

    private void close_confirmation(object sender, FormClosingEventArgs e)
    {
        if (e.CloseReason == CloseReason.FormOwnerClosing)
        {
            if (MessageBox.Show("Are you sure you want to close?", "Application", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
            {
                e.Cancel = true;
            }
        }
    }

您需要将事件分配给表单以使其工作:

this.FormClosing += new FormClosingEventHandler(close_confirmation);

如果您想让它停止,以便用户可以再次关闭窗口而不显示消息:

this.FormClosing -= close_confirmation;

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:

    private void close_confirmation(object sender, FormClosingEventArgs e)
    {
        if (e.CloseReason == CloseReason.FormOwnerClosing)
        {
            if (MessageBox.Show("Are you sure you want to close?", "Application", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
            {
                e.Cancel = true;
            }
        }
    }

You nee to assign the event to the form to make it work:

this.FormClosing += new FormClosingEventHandler(close_confirmation);

If you want to make it stop so the user can close again the window without the message:

this.FormClosing -= close_confirmation;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文