///
/// </summary>
/// <param name="mdiParent"></param>
/// <param name="frmtype"></param>
/// <returns></returns>
public static bool IsExistInstance(System.Windows.Forms.Form mdiParent, System.Type frmtype)
{
System.Windows.Forms.Form[] f = null;
int i = 0;
f = mdiParent.MdiChildren;
for (i = 0; i <= f.Length - 1; i++)
{
if (f[i].GetType().Equals(frmtype))
{
return true;
}
}
return false;
}
/// <summary>
///
/// </summary>
/// <param name="mdiParent"></param>
/// <param name="midChild"></param>
/// <returns></returns>
public static bool IsExistInstance(System.Windows.Forms.Form mdiParent, Form midChild)
{
System.Windows.Forms.Form[] f = null;
int i = 0;
f = mdiParent.MdiChildren;
for (i = 0; i <= f.Length - 1; i++)
{
if (object.ReferenceEquals(f[i], midChild))
{
return true;
}
}
return false;
}
/// <summary>
///
/// </summary>
/// <param name="mdiParent"></param>
/// <param name="frmtype"></param>
/// <returns></returns>
public static Form GetFormInstance(System.Windows.Forms.Form mdiParent, System.Type frmtype)
{
System.Windows.Forms.Form[] f = null;
int i = 0;
f = mdiParent.MdiChildren;
for (i = 0; i <= f.Length - 1; i++)
{
if (f[i].GetType().Equals(frmtype))
{
return f[i];
}
}
return null;
}
/// <summary>
///
/// </summary>
/// <param name="mdiParent"></param>
/// <param name="midChild"></param>
/// <returns></returns>
public static Form GetFormInstance(System.Windows.Forms.Form mdiParent, Form midChild)
{
System.Windows.Forms.Form[] f = null;
int i = 0;
f = mdiParent.MdiChildren;
for (i = 0; i <= f.Length - 1; i++)
{
if (object.ReferenceEquals(f[i], midChild))
{
return f[i];
}
}
return null;
}
调用:
//检查指定MDI窗体的实例是否存在。
IsExistInstance(this, typeof(WindowsFormsApplication2.Form1));
//或者
IsExistInstance(this, System.Type.GetType("WindowsFormsApplication2.Form1"));
is 检查对象是否与指定类型。