在WINFORM中,如何通过配置文件来配置窗体菜单并点击菜单能进入相应的窗体.
下面是两种方法:
第一种,适用于所有窗体都在同一个项目中
string formstring1 = @"当前命名空间的名字" + @"." + formName;//系统中窗体的名称
Type t = Type.GetType(formstring);
object obj = System.Activator.CreateInstance(t);
Form f = (Form)obj;
f.TopLevel = true;
f.ShowDialog();
Type t = Type.GetType(formstring);
object obj = System.Activator.CreateInstance(t);
Form f = (Form)obj;
f.TopLevel = true;
f.ShowDialog();
第二种,适用于窗体在不同的项目中
Assembly assembly = Assembly.LoadFile(Application.StartupPath + @"\AnotherProject.dll");
object temp = assembly.CreateInstance("AnotherProject.Form1");
Type curType = assembly.GetType("AnotherProject.Form1", false, true);
object obj = System.Activator.CreateInstance(curType);
Form f = (Form)obj;
f.TopLevel = true;
f.ShowDialog();
object temp = assembly.CreateInstance("AnotherProject.Form1");
Type curType = assembly.GetType("AnotherProject.Form1", false, true);
object obj = System.Activator.CreateInstance(curType);
Form f = (Form)obj;
f.TopLevel = true;
f.ShowDialog();