/// <summary>
/// 根据窗体Name打开窗体
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public Form FindForm(string name)
{
Assembly assembly = Assembly.GetExecutingAssembly();
// 实例化窗体
Form form = assembly.CreateInstance(DBConfig.GetConfigValue("Namespace") + name) as Form;
return form;
}
/// <summary>
/// 左侧菜单树点击
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void treeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (e.Node.Tag != null)
{
Form FormChild = FindForm(e.Node.Tag.ToString());
if (FormChild != null)
{
ShowChildForm(FormChild);
}
//else
//{
// MessageBoxEx.Show("当前窗体不存在");
//}
}
}
/// <summary>
/// 窗体切换
/// </summary>
/// <param name="form"></param>
private void ShowChildForm(Form form)
{
if (!FindTabPageForm(form.Name))
{
//获取能打开的窗体数
int sysTabPageNum =T2THelper.ToInt32(DBConfig.GetConfigValue("sysTabpageNum"), 5);
if (sysTabPageNum > tabControl1.TabPages.Count)
{
TabPage tp = new TabPage();
//将form的名称给tabpage
tp.Name = form.Name;
tp.Text = form.Text;
tabControl1.TabPages.Add(tp);
tabControl1.SelectedTab = tp;
form.FormBorderStyle = FormBorderStyle.None;
form.MdiParent = this;
//这比较关键,给出窗体的父容器为tabpage
form.Parent = tp;
form.Dock = DockStyle.Fill;
form.Show();
}
else
{
MessageBoxEx.Show("您好
只能同时打开" + sysTabPageNum.ToString() + "个窗口,请关于其他窗口后再打开", "提醒");
}
}
else
{
tabControl1.SelectedTab = GetTabPageForm(form.Name);
}
}
/// <summary>
/// 判断TabPage是否已经打开
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public bool FindTabPageForm(string name)
{
foreach (TabPage tp in tabControl1.TabPages)
{
if (name == tp.Name)
{
return true;
}
}
return false;
}
示例图