C#_启动多个窗体winform

启动多个窗体有很多方法,可以在Application.Run()中先实例化一个,然后在实例化出来的那个窗体的Onload,或者构造函数中实例化另一个窗体,这样在用户看来就是出现了两个窗体了。

MSDN上也有一种启动多个窗体的方法:

static class Program
{
    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        Application.Run(new MyapplicationContext());
    }
}

class MyapplicationContext : ApplicationContext
{
    private void onFormClosed(object sender, EventArgs e)
    {
        if (Application.OpenForms.Count == 0)
        {
            ExitThread();
        }
    }
    public MyapplicationContext()
    {
        var forms = new List<Form>(){
            new ServiceClient(),
            new frmInitial()
        };
        foreach (var form in forms)
        {
            form.FormClosed += onFormClosed;
        }
        foreach(var form in forms)
        {
            form.Show();
        }
    }
}
原文地址:https://www.cnblogs.com/pjCoder/p/5252611.html