zoukankan      html  css  js  c++  java
  • C# Winform同时启动多个窗体类

    首先创建一个类,存放将要同时显示的窗体

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        class MultiFormApplictionStart : ApplicationContext
        {
            /// <summary>
            /// 多窗口同时启动类
            /// <remarks>继承ApplicationContext的原因是Application.Run(ApplicationContext context);参数的需要</remarks>
            /// <remarks>另一个是关闭同时启动的窗口</remarks>
            /// </summary>
            private void onFormClosed(object sender, EventArgs e)
            {
                if (Application.OpenForms.Count == 0)
                {
                    ExitThread();
                }
            }
    
            public MultiFormApplictionStart()
            { 
                //将要显示的窗体集合
                var formList = new List<Form>(){
                    new Form1(),
                    new Form2()
                };
    
                foreach (var item in formList)
                {
                    item.FormClosed += onFormClosed;
                }
    
                foreach (var item in formList)
                {
                    item.Show();
                }
            }
        }
    }

    主程序Program更改为

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        static class Program
        {
            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new MultiFormApplictionStart());    //调用用于显示窗体的类
            }
        }
    }

    最终效果图

  • 相关阅读:
    TCP协议特点和三次握手/四次挥手
    CAP定理、BASE理论
    对自写的Asp.Net分页控件的应用方式(异步无刷新分页)
    Asp.Net分页控件
    SqlHelper
    简易贪吃蛇
    测试一下
    iOS --- DIY文件名批量修改
    iOS常用 --- NSDictionary 与 NSMutableDictionary
    iOS常用---NSArray,NSMutabuleArray
  • 原文地址:https://www.cnblogs.com/swjian/p/9663250.html
Copyright © 2011-2022 走看看