如何关闭Windows应用程序中的所有打开窗体 [英] How to close all the open forms in windows application

查看:130
本文介绍了如何关闭Windows应用程序中的所有打开窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



我在最后一个表单的按钮点击事件中写了下面的代码来关闭所有打开的表格



Hi all

I wrote the below code at the last form's button click event to close all the open forms

List<Form> openForms = new List<Form>();

            foreach (Form f in Application.OpenForms)
                openForms.Add(f);

            foreach (Form f in openForms)
            {
                f.Close();
            }





但是发生的事情是它没有关闭所有表格。例如如果我有6个开放表格,它只关闭3而休息3仍然是开放的。



请告诉我是什么问题或任何替代解决方案。



But what happening is it's not closing all the forms. e.g. if i have 6 open forms, it is closing only 3 and rest 3 is still open.

Please tell me what is the problem or any alternate solution.

推荐答案

假设这是一个WinForms应用程序,在Program.cs文件中以通常的方式启动:如果你做了关闭所有Open Forms,你会...在执行此操作的过程中...关闭应用程序的主窗体,并且应用程序将关闭,因此您尝试执行的操作与调用相同:



Application.Exit();



您的代码可能发生的异常是:如果您已经显示作为模态的表单...使用ShowDialog()...调用'关闭该表单并不真正处理它,在这种情况下,你需要在模态显示的表单上调用'Dispose()。但是,如果您调用代码关闭显示为-modal的一个Form范围内的所有Form,那么该假设才有意义。
Assuming this is a WinForms Application, started in the usual way in the Program.cs file: if you did close all Open Forms, you would ... in the process of doing that ... close the Application's Main Form, and, the Application would shut down, so what you are trying to do is the same thing as calling:

Application.Exit();

The exception that could happen with your code is: if you have "shown" a Form as modal ... using ShowDialog() ... calling 'Close on that Form does not really dispose of it, and, in that case, you need to call 'Dispose() on the modal-shown Form. But, that hypothesis only makes sense if you were calling your code to close all Forms inside the scope of the one Form shown as-modal.


这篇关于如何关闭Windows应用程序中的所有打开窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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