zoukankan      html  css  js  c++  java
  • 使用反射打开窗体,并控制一个窗体只能打开一个

    刚刚开始做CS项目,对于项目中的应用各种不熟悉。在MDI容器窗体上设置了一个菜单,菜单上每一个按钮可以打开一个子窗体。最初使用的方法是

    /// <summary>
    /// 选择面板
    /// </summary>
    private static OpenSelect os;
    
    private void OpenSelectForm()
    {
        if (os == null || os.IsDisposed)
        {
            os = new OpenSelect();
            os.MdiParent = this;
            os.Show();
        }
    }

    基本上打开每一个窗体都是这个做的。结果后台的代码很长很长,而且都是重复的。后来在整理代码的时候想起了很神奇的反射功能。于是,将代码更改成了下面

         public partial class MainForm : Form
        {
            public MainForm()
            {
                InitializeComponent();
                SetFormList();
            }
        
            /// <summary>
            /// 按钮的名称和窗体的命名空间和名称
            /// </summary>
            Dictionary<string, string> formList = new Dictionary<string, string>();
            /// <summary>
            /// 已经打开的窗体
            /// </summary>
            List<string> openList = new List<string>();
            /// <summary>
            /// 将按钮名称和窗体的命名空间及名称添加进集合
            /// </summary>
            private void SetFormList()
            {
                formList.Add("tsmNewDecive", "EmluatorSystem.NewDevice");
                formList.Add("tsmManageDevice", "EmluatorSystem.ManageDevice");
                formList.Add("TsmiCreateWave", "EmluatorSystem.CreateWave");
                formList.Add("TsmiSynch", "EmluatorSystem.WaveBuild");
                formList.Add("ToolItemAddTaskTempLate", "EmluatorSystem.TaskTempLateForm");
                formList.Add("toolCustTaskManage", "EmluatorSystem.CustTaskManage");
            }
    
            /// <summary>
            /// 菜单按钮
            /// </summary>
            private void MenuStripClick(object sender, EventArgs e)
            {
                ToolStripMenuItem tsmt = (ToolStripMenuItem)sender;
                if (formList.ContainsKey(tsmt.Name)) 
                {
                    if (!openList.Contains(formList[tsmt.Name]))
                    {
                        Assembly assembly = Assembly.GetExecutingAssembly();
                        object obj = assembly.CreateInstance(formList[tsmt.Name]);
                        Form f = (Form)obj;
                        f.FormClosing += new FormClosingEventHandler(f_FormClosing);
                        openList.Add(string.Format("EmluatorSystem.{0}", f.Name));
                        f.MdiParent = this;
                        f.Show();
                    }
                }
            }
    
            void f_FormClosing(object sender, FormClosingEventArgs e)
            {
                Form f = (Form)sender;
                openList.Remove(string.Format("EmluatorSystem.{0}", f.Name));
                f.FormClosing -= new FormClosingEventHandler(f_FormClosing);
            }
        }

    这个样子就可以用很短的代码控制很多个窗体,只要将所有的按钮的点击事件全部设置成为一个就可以了。不过由于对反射不是很了解,并不清楚这么做的优劣和效率,会不会出什么问题。
    所以如果有哪位仁兄真的要用的话请自己注意。当然了,如果发现了什么问题也请告诉我。



    想找一个无人的角落发呆……
  • 相关阅读:
    静态方法和类方法
    DEL: Restore Boxes after VirtualBox Upgrade
    DEL: IE "Your current security settings put your computer at risk. Click h
    EV: Using GitHub Repository
    EV: Windows Commands 命令
    EV: Notepad++ Regular Express syntax
    html页面的三个width: document, window, screen
    DEL: View web content zone in IE9
    EV: 关于min-width样式的使用
    EV: Linux Shell Commands
  • 原文地址:https://www.cnblogs.com/rogation/p/2729541.html
Copyright © 2011-2022 走看看