C#MDI中防止子窗体被多次实例化
第一种方式:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
if (gongChenJinDuFrm == null || gongChenJinDuFrm.IsDisposed)
{
gongChenJinDuFrm = new GongChenJinDuFrm();
}
gongChenJinDuFrm.Text = "工程进度--" + DGJName;
gongChenJinDuFrm.FillData(DGJID);
gongChenJinDuFrm.MdiParent = this;
gongChenJinDuFrm.WindowState = FormWindowState.Normal;
gongChenJinDuFrm.MaximizeBox = false;
gongChenJinDuFrm.StartPosition = FormStartPosition.CenterScreen;
gongChenJinDuFrm.Show();
第二种方式:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
private static GongChenJinDuFrm gongChenJinDuFrm;
private GongChenJinDuFrm GetGongChenJinDuFrm()
{
//先后次序不能错,前一个条件用来判断是否是第一次,后一个条件用来判断子窗体是否被关闭了
if (gongChenJinDuFrm == null || gongChenJinDuFrm.IsDisposed)
{ //第一次实例化也好,被关了也好,都重新实例化
gongChenJinDuFrm = new GongChenJinDuFrm();
gongChenJinDuFrm.MdiParent = this;
}
return gongChenJinDuFrm;
}