子窗体不重复打开和再次打开问题
问题描述:
我在主窗体F_Main中打开子窗体F_Child,为了不重复打开F_Child,我设置了如下代码:
public static F_Child Frm_Child=null;//首先在F_Main中申明一个全局变量,记录子窗体是否打开;
当主窗体打开子窗体时,作如下判读:
if(Frm_Child=null)
{
Frm_Child=new F_Child();
Frm_Child.mdiparent=this;
Frm_Child.show;
}
else
{
Frm_Child.Activate();
}
这个代码能够实现子窗体不重复打开。问题是子窗体关闭过后,再打开子窗体时就显示不出来了????请高手指点怎么样实现即可不不重复打开,又可以关闭后再打开???
[解决办法]
public partial class ShowImagesForm : Form {private static ShowImagesForm sForm; private ShowImagesForm() { InitializeComponent(); } public static ShowImagesForm GetForm() { if (sForm==null) { sForm = new ShowImagesForm(); } return sForm; } private void ShowImagesForm_FormClosed(object sender, FormClosedEventArgs e) { sForm = null; }
[解决办法]
private void ShowChildFrom(Form chfr, string fromname) { bool isfind = false; foreach (Form fm in Application.OpenForms) { if (fm.Name == fromname) { fm.Activate(); fm.WindowState = FormWindowState.Normal; return; } } if (!isfind) { Form fm = chfr; fm.Show(); } } private void ShowChildFrom(Form chfr, string fromname) { bool isfind = false; foreach (Form childrenForm in this.MdiChildren) { if (childrenForm.Name == fromname) { childrenForm.Visible = true; childrenForm.Activate(); return; } } if (!isfind) { Form fm = chfr; fm.Show(); } }