代码改变世界

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

2013-05-06 11:33  苏飞  阅读(6499)  评论(0编辑  收藏  举报
/// <summary>  
        /// 打开新的子窗体  
        /// </summary>  
        /// <param name="strName">窗体的类名</param>  
        /// <param name="AssemblyName">窗体所在类库的名称</param>   
        public static void CreateForm(string strName, string AssemblyName)
        {
            string path = AssemblyName;//项目的Assembly选项名称  
            string name = strName; //类的名字  
            Form doc = (Form)Assembly.Load(path).CreateInstance(name);
            doc.Show();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //获取单击的Button名称
            string btnname = ((Button)sender).Text;
            CreateForm("WindowsFormsApplication1." + btnname, "WindowsFormsApplication1");
        }

 原文:http://www.sufeinet.com/thread-2984-1-1.html