.NET MDI(only one form)

1.Show method.
int index = Opened(this, "FrmMytest");

if (index == -1)


{


FrmMytest frmMytest = new FrmMytest();


 frmMytest.MdiParent = this;


frmMytest.Show();


}


else

{

this.MdiChildren[index].Show();

}


public static int Opened(Form frmMdiFather, string strMdiChild)
{
int bReturn = -1;
for (int i = 0; i < frmMdiFather.MdiChildren.Length; i++)
{
if (frmMdiFather.MdiChildren[i].Name == strMdiChild)
{
frmMdiFather.MdiChildren[i].BringToFront();
bReturn = i;
break;
}
}
return bReturn;
}


2.use singleton method
Mainform中调用的代码:
try
{
FrmLot objfrmbl = FrmLot.getInstance();
objfrmbl.MdiParent = this;
objfrmbl.WindowState = FormWindowState.Maximized;
objfrmbl.Show();
objfrmbl.BringToFront();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

FrmLot 的方法及实例化:
private string MaterialName = String.Empty;
private string Route = String.Empty;
private string RouteVersion = String.Empty;
private string StepOperationName = String.Empty;
private string StepName = String.Empty;
private static FrmLot objfrmCL = null;

protected FrmLot()
{
InitializeComponent();
}

public static FrmLot getInstance()
{
if (objfrmCL == null)
{
lock (typeof(FrmLot))
{
if (objfrmCL == null)
{
objfrmCL = new FrmLot();
}
}
}
return objfrmCL;
}

你可能感兴趣的:(.NET)