我的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个表单?
答案 0 :(得分:18)
通常,让应用程序执行除默认操作之外的其他操作(打开表单,等待它关闭然后退出)的正确方法是创建一个继承自ApplicationContext
的类。然后将类的实例传递给Application.Run
方法。应用程序关闭后,请在班级内拨打ExitThread()
。
在这种情况下,您可以在应用程序加载时创建三个表单的实例,并为其Closed
事件注册处理程序。当每个表单关闭时,处理程序将检查是否还有其他表单仍然打开,如果没有则关闭应用程序。
The example on MSDN正在做两件事:
一个更简单的示例,仅在关闭所有表单后关闭应用程序:
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<Form>() {
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());
}
}
应用程序关闭逻辑显然可以自定义 - 任何表单仍然是打开的,或者只是这三种类型中的一种,或者只是前三个实例(需要保存对前三个实例的引用,可能在{{ 1}})。
Re:每个表单创建的全局事件 - this看起来很有希望。
类似的例子here。
答案 1 :(得分:10)
从Form1
private void Form1_Load(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
}
事件开始其他表单。
{{1}}
答案 2 :(得分:-1)
我通过稍微修改Zev Spitz的答案找到了我的解决方案。
注意:此版本仅启动“表单”列表中指定的第一个表单。
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;
class MyApplicationContext : ApplicationContext
{
public List<Form> Forms = new List<Form>() {
new Form1()
};
private List<Form> FormCollectionToList(FormCollection fc)
{
List<Form> ff = new List<Form>();
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<Form> Forms = new List<Form>() {
new Form1()
};
private List<Form> FormCollectionToList(FormCollection fc)
{
List<Form> ff = new List<Form>();
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 :(得分:-2)
你也可以使用3个标签而不是3个表格