C#反射动态调用窗体,动态实例化窗体的方法

来源:互联网 发布:php云人才系统破解版 编辑:程序博客网 时间:2024/05/20 09:06

其实这个问题很简单只要我们使用一点点反射的原理就行了,


单击那个按钮就会出现那个窗体,
如果非常多的按钮的话我们是不是一般会这样写

publicstaticvoidCreateForm(stringstrName,stringAssemblyName)

    {  

stringpath = AssemblyName;//项目的Assembly选项名称  

 stringname = strName; //类的名字 

  Form doc = (Form)Assembly.Load(path).CreateInstance(name);

doc.Show();

      }
 
      privatevoid button1_Click(objectsender, EventArgs e)
      {
          //获取单击的Button名称
          stringbtnname = ((Button)sender).Text;
          CreateForm("WindowsFormsApplication1."+ btnname, "WindowsFormsApplication1");
      }
UCBasicInfo.dll|UCBasicInfo.ucUpdate
  /// <summary>打开窗体</summary>        public void openClientForm(string windowName, string dllName, string className)        {            try            {                System.Type type = Assembly.LoadFrom(AppDomain.CurrentDomain.BaseDirectory + dllName).GetType(className, false);                if (type == null)                {                    throw new Exception("未能加载菜单项" + windowName + ",请联系系统管理员!");                }                ucClientBase ucClient = (ucClientBase)Activator.CreateInstance(type);                if (ucClient == null)                {                    return;                }                ucClient.client = this;                ucClient.Name = windowName;                ucClient.Dock = DockStyle.Fill;                TabPage pg = new TabPage();                pg.Tag = tcMain.SelectedTab;//记住之前打开的窗体,当改窗体关闭的时候跳到前一个窗体                pg.Text = windowName;                pg.Controls.Add(ucClient);                              if (!tabControlCheckHave(tcMain, pg.Text))                {                    tcMain.TabPages.Add(pg);                    tcMain.SelectedTab = pg;                }                else                {                    pg.Dispose();                }            }            catch (Exception ex)            {                throw ex;            }        }


0 0
原创粉丝点击