Sunday, 18 August 2013

Translate Code to Class

Translate Code to Class

I have this in my MDI parent:
Form1 newForm1 = new Form1();
private void MDIParent1_Load(object sender, EventArgs e)
{
newForm1 = null;
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
if (newForm1== null)
{
newForm1 = new Form1();
newForm1.MdiParent = this;
newForm1.FormClosed += new
FormClosedEventHandler(newForm1_FormClosed);
newForm1.Show();
}
else
newForm1.Activate();
}
void newForm1_FormClosed(object sender, FormClosedEventArgs e)
{
newForm1 = null;
}
How I will translate this to a class? It would be something like this:
public static void openForm(Form newForm, FormInstance Instance)
{
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;
}
}
So that I will have this:
Form1 newForm1 = new Form1();
Form2 newForm2 = new Form2();
private void MDIParent1_Load(object sender, EventArgs e)
{
newForm1 = null;
newForm2 = null;
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
openForm(Form1, newForm1);
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
openForm(Form2, newForm2);
}

No comments:

Post a Comment