/// <summary>判断窗体是否已存在实例 /// /// </summary> /// <param name="type">窗体类型</param> /// <returns></returns> public static Form ExistedForm(Type type) { return Application.OpenForms.Cast<Form>().FirstOrDefault(form => form.GetType() == type); }
使用方法:
Form isExist = ExistedForm(typeof(Form1)); if (isExist == null) { Form1 Frm = new Form1(); Frm.Show(); } else { isExist.WindowState = FormWindowState.Normal; isExist.Activate(); }
窗体仅显示一个
FrmInfo frmInfo = GenericSingleton<FrmInfo>.CreateInstrance();
frmInfo.Show(this);