上一个只是简单实现一个一个窗体的展示
如何利用treeview来实现不同节点展示不同内容
一个比较基础的方法利用点击的treeView的索引不同,跳转至不同页面
private static FrmBaseManager frmBase;
private static FrmUserManager frmUser;
private static FrmNull frmNull;
private static Form form;
private static List<Form> formList=new List<Form>();
public static Form CreatorForm(int index)
{
HideFormAll();//隐藏已有的窗体
switch (index)
{
case 0:
if (frmUser == null)
{
frmUser = new FrmUserManager();
formList.Add(frmUser);
}
form = frmUser;
break;
case 1:
if (frmBase == null)
{
frmBase = new FrmBaseManager();
formList.Add(frmBase);
}
form = frmBase;
break;
case 2:
if (frmNull == null)
//还未创建的窗体统一用FrmNull或者直接在default处添加
{
frmNull = new FrmNull();
formList.Add(frmNull);
}
form = frmNull;
break;
case 3:
if (frmNull == null)
{
frmNull = new FrmNull();
formList.Add(frmNull);
}
form = frmNull;
break;
default:
break;
}
return form;
}
public static void HideFormAll()
{
foreach (Form f in formList)
{
f.Hide();
}
}
在主窗体中 tree view的方法中
Form form = FormFactory.CreatorForm(e.Node.Index);
form.MdiParent = this;
form.Parent = splitContainer1.Panel2;
form.Show();
效果展示:切换不同的treeView节点跳转不同界面
如果遇到功能较多的项目,需要转换的窗体较多时这种方法就不适合了
非常麻烦且笨重
一个更好的方法
在treeView中给对应节点的Tag加上对应窗体 的名字,如下图所示
Assembly.Load("项目名"); 获取指定程序集的属性方法等,由此通过对比找到对应窗口
下列两个加粗方法与前一个获得结果一样
string path = AppDomain.CurrentDomain.BaseDirectory;
Assembly assembly = Assembly.LoadFile(path+ "InternPro.exe")
Assembly assembly = Assembly.LoadFrom("InternPro.exe");
forname=forname==null?"FrmNull":forname; 如果传入的tag内容为空则默认为FrmNull窗体
private static List<Type> types;
static FormFactory()
{
Assembly assembly = Assembly.Load("InternPro");
types = assembly.GetTypes().ToList();
}
public static Form CreatorForm(string forname)
{
string path = AppDomain.CurrentDomain.BaseDirectory;
//Assembly assembly = Assembly.LoadFile(path+ "InternPro.exe")
// Assembly assembly = Assembly.LoadFrom("InternPro.exe");
HideFormAll();
forname=forname==null?"FrmNull":forname;
Form form= formList.Find(m=>m.Name == forname);
if (form == null)
{
Type type = types.Find(m => m.Name == forname);
form = (Form)Activator.CreateInstance(type);
formList.Add(form);
}
return form;
}
Form form=FormFactory<int>.CreatorForm(e.Node.Tag?.ToString());
form.MdiParent = this;
form.Parent = splitContainer1.Panel2;
form.Show();