Winform Tip 1: Open the window through the window class name + reflection

origin

When developing Winform, Mdi has many sub-windows, of course, there are different sub-window class names, such as FrmWin1, FrmWin2, FrmWin3, because some judgments are needed when opening these windows, as follows:
1) Judge whether FrmWin1 is open, if not Open FrmWin1, otherwise activate FrmWin1 to prevent FrmWin1 from being opened repeatedly.
2) Because there are many FrmWin classes, which are new every time, it feels inconvenient and the amount of code is large. I originally tried to open the window with "FrmWin class name + reflection". After completing the project, I forgot.
This time I am going to make a small program, so I picked it up and recorded it so that I won't forget it again.
OK, the source code is directly below.

Source code

ControlHelper.cs class

 /// <summary>
        /// 判断子窗体是否打开
        /// </summary>
        /// <param name="formName"></param>
        /// <returns></returns>
        public static bool MdiChildWinIsOpened(this Form parentFrm, string frmName)
        {
    
    
            bool isOpen = false;

            //判断父窗口IsMdiContainer是否true,否则设置true
            if (!parentFrm.IsMdiContainer) parentFrm.IsMdiContainer = true;

            foreach (Form childrenForm in parentFrm.MdiChildren)
            {
    
    
                // 检测是不是当前子窗口名称
                if (childrenForm.Name == frmName)
                {
    
    
                    // 是,显示
                    childrenForm.Visible = true;
                    // 激活
                    childrenForm.Activate();
                    //childrenForm.WindowState = FormWindowState.Maximized;
                    isOpen = true;
                }
            }
            return isOpen;
        }

        /// <summary>
        /// 根据窗口名称打开Mdi子窗口
        /// </summary>
        /// <param name="parentFrm">父窗口,默认this</param>
        /// <param name="frmClassFullName">Form类的全名称:命名空间名.类名   </param>
        /// <param name="assembly">程序集</param>
        public static void MdiChildWinOpen(this Form parentFrm,  string frmName, Assembly assembly)
        {
    
    

            // 判断子窗体是否打开, 如果没有打开
            if (MdiChildWinIsOpened(parentFrm, frmName) == false )
            {
    
    
                //Assembly assembly = Assembly.GetExecutingAssembly();

                // 拼接成class的全名称:命名空间名.类名
                // 通过,分割,取第一个字符串strList[0]为"命名空间名" 
                string[] strList = assembly.FullName.Split(',');
                string frmFullName = string.Format("{0}.{1}", strList[0], frmName);

                // 根据class的全名称,实例化窗体
                Form frm = assembly.CreateInstance(frmFullName) as Form;

                // 设置Mdi的Father窗口
                frm.MdiParent = parentFrm;
                frm.WindowState = FormWindowState.Maximized;
                frm.Show();
            }
        }

To use it, just call the method
ControlHelper.MdiChildWinOpen() in the menu Clicked event of the main window FrmMain , as follows:

// 打开FrmWin1窗口
ControlHelper.MdiChildWinOpen(this, "FrmWin1", Assembly.GetExecutingAssembly());

// 打开FrmWin2窗口
ControlHelper.MdiChildWinOpen(this, "FrmWin2", Assembly.GetExecutingAssembly());

Isn't it easy to use this way! !

Guess you like

Origin blog.csdn.net/coolhe21cn/article/details/104021033