winform的,先判断父窗口下有无同类型的子窗体,有则激活之,无则新建之
public static void newPForm(Form thisForm)
{
Form newForm = null;
foreach (Form f in thisForm.MdiParent.MdiChildren)
{
if (f is frmP)//关键是这里, frmP是一个class名,class名不能直接当作参数传递
{
newForm = f;
break;
} }
if (newForm == null)
{
newForm = new frmP();
newForm.MdiParent = thisForm.MdiParent;
newForm.Show();
}
else
{
newForm.Activate();
}
}
public static void newIForm(Form thisForm)
{
Form newForm = null;
foreach (Form f in thisForm.MdiParent.MdiChildren)
{
if (f is frmIRegister)
{
newForm = f;
break;
} }
if (newForm == null)
{
newForm = new frmIRegister();
newForm.MdiParent = thisForm.MdiParent;
newForm.Show();
}
else
{
newForm.Activate();
}
}
public static void newCForm(Form thisForm)
{
Form newForm = null;
foreach (Form f in thisForm.MdiParent.MdiChildren)
{
if (f is frmC)
{
newForm = f;
break;
} }
if (newForm == null)
{
newForm = new frmC();
newForm.MdiParent = thisForm.MdiParent;
newForm.Show();
}
else
{
newForm.Activate();
}
}
public static void newPForm(Form thisForm)
{
Form newForm = null;
foreach (Form f in thisForm.MdiParent.MdiChildren)
{
if (f is frmP)//关键是这里, frmP是一个class名,class名不能直接当作参数传递
{
newForm = f;
break;
} }
if (newForm == null)
{
newForm = new frmP();
newForm.MdiParent = thisForm.MdiParent;
newForm.Show();
}
else
{
newForm.Activate();
}
}
public static void newIForm(Form thisForm)
{
Form newForm = null;
foreach (Form f in thisForm.MdiParent.MdiChildren)
{
if (f is frmIRegister)
{
newForm = f;
break;
} }
if (newForm == null)
{
newForm = new frmIRegister();
newForm.MdiParent = thisForm.MdiParent;
newForm.Show();
}
else
{
newForm.Activate();
}
}
public static void newCForm(Form thisForm)
{
Form newForm = null;
foreach (Form f in thisForm.MdiParent.MdiChildren)
{
if (f is frmC)
{
newForm = f;
break;
} }
if (newForm == null)
{
newForm = new frmC();
newForm.MdiParent = thisForm.MdiParent;
newForm.Show();
}
else
{
newForm.Activate();
}
}
if (f is frmC)
{
newForm = f;
break;
if (newForm == null)
{
newForm = new frmC();
newForm.MdiParent = thisForm.MdiParent;
newForm.Show();
} }
else if(f is frmB)
{
newForm = f;
break;}
{
Type t = typeof(t2);
if (f is t) { }
}
//最后查找资料后结果是这样的,可能代码有些冗余
#region 查找并打开子窗体函数
/// <summary>
/// 根据子窗体的名称类型打开子窗体
/// </summary>
/// <param name="childTypeString">子窗体名称类型</param>
public static void findAndOpenWindow(Form thisForm, string childTypeString)
{
Form myChild = null;
if (!containMDIChild(thisForm,childTypeString))
{
try
{
Assembly assembly = Assembly.GetExecutingAssembly();
// Create data type using type string
Type typForm = assembly.GetType(childTypeString); // Create object using type's "InvokeMember" method
Object obj = typForm.InvokeMember(
null,
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.CreateInstance,
null,
null,
null); // Show child form
if (obj != null)
{
myChild = obj as Form;
myChild.MdiParent = thisForm.MdiParent;
myChild.WindowState = FormWindowState.Maximized;
myChild.Show();
myChild.Focus();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
//MessageBox.Show("未能加载程序模块", "警告!");
}
}
}
/// <summary>
/// 根据子窗体的名称类型查找子窗体
/// </summary>
/// <param name="ChildTypeString">子窗体的名称类型</param>
/// <returns></returns>
private static bool containMDIChild(Form thisForm, string childTypeString)
{
Form myMDIChild = null;
foreach (Form f in thisForm.MdiParent.MdiChildren)
{
string a = f.GetType().ToString();
if (f.GetType().ToString() == childTypeString)
{
myMDIChild = f;
break;
}
} // 显示已经存在的窗体
if (myMDIChild != null)
{
/*
myMDIChild.TopMost = true;
myMDIChild.WindowState = FormWindowState.Maximized; myMDIChild.Show();
myMDIChild.Focus();
*/
myMDIChild.Activate();
return true;
}
else
{
return false;
}
}
#endregion