在主窗体退出时没有弹出窗口的窗体关闭(阻止弹出窗口并强行关闭其他窗体) [英] Forms closing without pop ups(blocking popup and forcefully closing other forms) on main form exit

查看:26
本文介绍了在主窗体退出时没有弹出窗口的窗体关闭(阻止弹出窗口并强行关闭其他窗体)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果这个问题是重复的,请接受我的道歉.我搜索了现有的问题和解决方案,但没有找到我的问题的确切解决方案.

Please accept my apologies,if this question is repeatative. I searched existing questions and solutions and I didn't find exactly solution to my problem.

我正在开发 winform 应用程序,我有一个主要的 form1,它创建了 form2.Form2 有关闭按钮和覆盖的 FormClosing() 方法来提示确认弹出窗口,效果很好.

I'm developing winform application, I have a main form1, which creates form2. Form2 has close button and overidden FormClosing() method to prompt confirmation popup,which works perfectly.

当我想关闭 form1(它是主应用程序线程)并且我想关闭所有其他表单时,比如由 form1 创建的 form2、form3,而不实际提示 form2、form3 的确认弹出窗口.基本上,当关闭 form1 时,它应该强制关闭从 form1 显示的所有其他表单,现在它会在退出应用程序之前显示 form2、form3 的所有确认弹出窗口.

When I want to close form1(which is main application thread) and I want to close all other forms say form2, form3 which were created by form1, without actually prompting confirmation popups for form2, form3. Basically when close form1 it should forcelly close all other forms which were shown from form1, right now it shows all confirmation popups for form2, form3 before exiting application.

感谢您的回复,问候skm

I thank for your reply, regards skm

推荐答案

您需要注意在 FormClosing 事件中传递给您的 e.CloseReason 属性值.仅在 e.CloseReason == CloseReason.UserClosing 时提示用户.这也确保您在用户关闭 Windows 时不显示对话框.例如:

You'll need to pay attention to the e.CloseReason property value that's passed to you in the FormClosing event. Only prompt the user if e.CloseReason == CloseReason.UserClosing. This also ensures that you don't display the dialog when the user shuts down Windows. For example:

    private void Form2_FormClosing(object sender, FormClosingEventArgs e) {
        if (e.CloseReason == CloseReason.UserClosing && !saved) {
            switch (MessageBox.Show(this, "Save changes?", "Closing",
                    MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)) {
                case DialogResult.Yes: Save(); break;
                case DialogResult.No: break;
                case DialogResult.Cancel: e.Cancel = true;
            }
        }
    }

务必考虑始终保存更改,以免用户遭受意外数据丢失.对一个临时文件说,当程序启动备份时,您会重新打开该文件.

Do consider always saving changes so the user won't suffer from unexpected data loss. Say to a temporary file that you re-open when the program starts back up.

这篇关于在主窗体退出时没有弹出窗口的窗体关闭(阻止弹出窗口并强行关闭其他窗体)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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