C#应用程序启动时显示多个表单

我的C#应用​​程序中有3个表单 – Form1,Form2和Form3。 目前,当我的应用程序启动时,它会加载Form1。 我想在应用程序启动时打开所有三个表单。

我试过在Program.cs这样做:

 static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); Application.Run(new Form2()); } 

但Form2仅在Form1关闭后显示。

如何在应用程序启动后立即同时显示所有3个表单?

Form1Form.Load事件启动其他窗体。

 private void Form1_Load(object sender, EventArgs e) { Form2 form2 = new Form2(); form2.Show(); } 

通常,让应用程序执行除默认操作之外的其他操作(打开表单,等待它关闭然后退出)的正确方法是创建一个inheritance自ApplicationContext的类。 然后将类的实例传递给Application.Run方法。 当应用程序关闭时,从您的类中调用ExitThread()

在这种情况下,您可以在加载应用程序时创建三个表单的实例,并为其Closed事件注册处理程序。 当每个表单关闭时,处理程序将检查是否还有其他表单仍然打开,如果没有则关闭应用程序。

MSDN上的示例做了两件事:

  1. 打开多个表单并在应用程序全部关闭时退出
  2. 在每个表单关闭时保存表单的最后大小和位置。

一个更简单的示例,仅在关闭所有表单后关闭应用程序:

 class MyApplicationContext : ApplicationContext { private void onFormClosed(object sender, EventArgs e) { if (Application.OpenForms.Count == 0) { ExitThread(); } } public MyApplicationContext() { //If WinForms exposed a global event that fires whenever a new Form is created, //we could use that event to register for the form's `FormClosed` event. //Without such a global event, we have to register each Form when it is created //This means that any forms created outside of the ApplicationContext will not prevent the //application close. var forms = new List
() { new Form1(), new Form2(), new Form3() }; foreach (var form in forms) { form.FormClosed += onFormClosed; } //to show all the forms on start //can be included in the previous foreach foreach (var form in forms) { form.Show(); } //to show only the first form on start //forms[0].Show(); } }

然后,您的Program类如下所示:

 static class Program { [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new MyApplicationContext()); } } 

应用程序关闭逻辑显然可以自定义 – 任何表单仍然是打开的,或者只是这三种类型中的一种,或者只有前三个实例(需要保存对前三个实例的引用,可能在List

) 。

Re:每个表单创建的全局事件 – 这看起来很有希望。

这里有类似的例子。

我通过稍微修改Zev Spitz的答案找到了我的解决方案。

注意:此版本仅启动“表单”列表中指定的第一个表单。

 using System; using System.Collections.Generic; using System.Diagnostics; using System.Windows.Forms; class MyApplicationContext : ApplicationContext { public List
Forms = new List
() { new Form1() }; private List
FormCollectionToList(FormCollection fc) { List
ff = new List
(); foreach (Form f in fc) { ff.Add(f); } return ff; } private void onFormClosed(object sender, EventArgs e) { Forms = FormCollectionToList(Application.OpenForms); if (Forms.Count == 0) { ExitThread(); } foreach (var form in Forms) { form.FormClosed -= onFormClosed; form.FormClosed += onFormClosed; } } public MyApplicationContext() { if (Forms.Count == 0) { Process.GetCurrentProcess().Kill(); } Forms[0].FormClosed += onFormClosed; Forms[0].Show(); } }

这个版本启动了所有这些。

 using System; using System.Collections.Generic; using System.Diagnostics; using System.Windows.Forms; class MyApplicationContext : ApplicationContext { public List
Forms = new List
() { new Form1() }; private List
FormCollectionToList(FormCollection fc) { List
ff = new List
(); foreach (Form f in fc) { ff.Add(f); } return ff; } private void onFormClosed(object sender, EventArgs e) { Forms = FormCollectionToList(Application.OpenForms); if (Forms.Count == 0) { ExitThread(); } foreach (var form in Forms) { form.FormClosed -= onFormClosed; form.FormClosed += onFormClosed; } } public MyApplicationContext() { if (Forms.Count == 0) { Process.GetCurrentProcess().Kill(); } foreach (var form in Forms) { form.FormClosed += onFormClosed; form.Show(); } } }

你也可以使用3个标签而不是3个表格