c# - 如何在 Windows 窗体应用程序中关闭多个窗体

标签 c# .net winforms visual-studio visual-studio-2015

我在 Visual Studio 2015 中使用 Windows 窗体应用程序。现在我的要求是在单击按钮时关闭多个窗口窗体。

注意:我引用了以下链接

closing-multiple-forms-in-c-sharp

How-to-Connect-forms-in-Csharp-windows-application

How to swap between forms in windows forms application

检查下面的图像我的工作。

图片 - 1 enter image description here

图片 - 2 enter image description here

图片 - 3 enter image description here

图片 - 4 enter image description here

最佳答案

您创建了第一个表单的另一个实例并将其隐藏,因此当前打开的第一个表单不会隐藏。

您应该隐藏打开的实例。为此,您可以使用 Application.OpenForms 找到它:

var first = Application.OpenForms.OfType<FirstForm>().FirstOrDefault();
if (first != null)
    first.Hide();

您还可以在 Program 类中保留对第一个表单的引用,并使用该引用来隐藏它或再次显示它。

请注意,Application.OpenForms 返回可见表单,如果您隐藏表单,则该表单将不再出现在集合中。

关于c# - 如何在 Windows 窗体应用程序中关闭多个窗体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36275315/

相关文章:

c# - 在应用程序启动时禁用 Windows 服务

c# - 自定义DataGridViewRow问题

c# - 表达式树中的 ArrayAccess 与 ArrayIndex

.net - 简单的 .NET XML 库

c# - Metro App 不能再以编程方式被杀死?

C# 在标签周围绘制圆圈

.net - 使用设计器支持将表单作为属性添加到用户控件

c# - 订阅在 Func<> 中引发的事件

c# - UWP 单元测试调试 "Unable to attach to CoreCLR"

c# - 将自定义编辑器窗口添加到 Visual Studio 窗口 Pane