-
子父窗体的设置 以及判断是否其子窗体,存在子窗体是否Activate显示获取焦点。 呵呵 交流一下 有心得的朋友请探讨给点建议。
-
public class FormHelper
{
/// <summary>
/// 设置子窗体
/// </summary>
/// <param name="type">类型</param>
/// <param name="parent">父窗体</param>
/// <returns></returns>
public static Form ShowForm(Type type, Form parent)
{
Form retForm;
if (!ChildExists(type, parent, true, out retForm))
{
ConstructorInfo[] cons = type.GetConstructors();
if (cons.Length > 0)
{
Object obj =cons[0].Invoke(null);
if (obj is Form)
{
retForm = obj as Form;
retForm.MdiParent = parent;
retForm.Show();
retForm.Activate();
}
}
}
return retForm;
}
/// <summary>
/// 子窗体是否存在
/// </summary>
/// <param name="type">类型</param>
/// <param name="parent">父窗体</param>
/// <param name="showForm">是否显示</param>
/// <param name="form">子窗体</param>
/// <returns></returns>
public static bool ChildExists(Type type, Form parent, bool showForm, out Form form)
{
for (int i = 0; i < parent.MdiChildren.Length; i++)
{
Form child = parent.MdiChildren[i];
if (child.GetType() == type)
{
if (showForm)
{
child.Visible = true;
child.Activate();
}
form = child;
return true;
}
}
form = null;
return false;
}
} -
Form f = Application.OpenForms["窗体名称"]; //查找窗体是否打开 存在则返回 窗体对象 否则null