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

时间:2012-11-15 20:57:24

标签: c# winforms

我的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个表单?

4 个答案:

答案 0 :(得分:18)

通常,让应用程序执行除默认操作之外的其他操作(打开表单,等待它关闭然后退出)的正确方法是创建一个继承自ApplicationContext的类。然后将类的实例传递给Application.Run方法。应用程序关闭后,请在班级内拨打ExitThread()

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

The example on MSDN正在做两件事:

  1. 打开多个表单并在应用程序全部关闭时退出
  2. 在关闭每个表单时保存表单的最后大小和位置。
  3. 一个更简单的示例,仅在关闭所有表单后关闭应用程序:

    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个表格