我的 MDI 父母有这个:
Form1 newForm1 = new Form1(); //newForm1 is the instance of Form1
private void MDIParent1_Load(object sender, EventArgs e)
{
newForm1 = null; //newForm1 is set to null
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
if (newForm1== null)
{
// if Form1 is not yet open it will be open
newForm1 = new Form1();
newForm1.MdiParent = this;
newForm1.FormClosed += new FormClosedEventHandler(newForm1_FormClosed); //add event handler when the form close
newForm1.Show();
}
else
//if Form1 is already open it will just be activate
newForm1.Activate();
}
void newForm1_FormClosed(object sender, FormClosedEventArgs e)
{
newForm1 = null; //when the Form1 Closed the newForm1 will be set to null
}
我将如何将其翻译为课程?它会是这样的:
public static void openForm(Form newForm, FormInstance Instance) //newForm is the name of the Form, Instance is the instance of that form
{
if (Instance == null)
{
Instance = new newForm();
Instance.MdiParent = this;
Instance.FormClosed += new FormClosedEventHandler(Instance_FormClosed);
Instance.Show();
}
else
Instance.Activate();
}
void Instance_FormClosed(object sender, FormClosedEventArgs e)
{
Instance = null;
}
所以我会有这个:
Form1 newForm1 = new Form1(); //newForm1 is the instance of Form1
Form2 newForm2 = new Form2(); //newForm2 is the instance of Form2
private void MDIParent1_Load(object sender, EventArgs e)
{
newForm1 = null; //newForm1 will set to null
newForm2 = null; //newForm2 will set to null
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
openForm(Form1, newForm1); //Form1 is the name of the Form, newForm1 is the instance of Form1
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
openForm(Form2, newForm2); //Form2 is the name of the Form, newForm2 is the instance of Form2
}