当“ X”出现时要求确认点击按钮 [英] Asking for confirmation when "X" button is clicked

查看:61
本文介绍了当“ X”出现时要求确认点击按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是带有确定要关闭吗?的消息框确实弹出,但是当我单击否时,它仍会继续关闭程序。有什么建议?
这是我的代码:

The problem is that the messagebox with "sure you wanna close?" does pop up, but when I click "no", it still proceeds to close the program. Any suggestions? Here's my code:

    protected override void OnFormClosing(FormClosingEventArgs e)
    {
        CloseCancel();
    }

    public static void CloseCancel()
    {
        const string message = "Are you sure that you would like to cancel the installer?";
        const string caption = "Cancel Installer";
        var result = MessageBox.Show(message, caption,
                                     MessageBoxButtons.YesNo,
                                     MessageBoxIcon.Question);

        if (result == DialogResult.Yes)
            Environment.Exit(0);
    }


推荐答案

您应该设置 取消 FormClosingEventArgs 参数的> 属性设置为 true 。通常,不需要 显式的 Environment.Exit(0),因为表单正以任何方式关闭(取消

You are expected to set the Cancel property of the FormClosingEventArgs argument to true when you require the close-operation to be cancelled. And an explicit Environment.Exit(0) is normally not required since the form is on its way to being closed any way (the cancellation of the shutdown process is opt-in, not opt-out).

将最后一位替换为:

var result = MessageBox.Show(message, caption,
                             MessageBoxButtons.YesNo,
                             MessageBoxIcon.Question);

e.Cancel = (result == DialogResult.No);

这篇关于当“ X”出现时要求确认点击按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆