How do I create a function for following code so that i may not have to write the following whole code to make a form be used as MDICHILD Form.
Students stu = null;
private void studentsToolStripMenuItem1_Click(object sender, EventArgs e)
{
if (stu == null || stu.IsDisposed)
{
stu = new Students();
stu.MdiParent = this;
stu.Show();
}
else
{
stu.Activate();
}
}
while I want it like this
private void studentsToolStripMenuItem1_Click(object sender, EventArgs e)
{
CreateMdiChild(Students);
}
and function should be like this
public void CreateMdiChild(Form form)
{
//expected code
}
You could make the method generic, e.g. :
Usage:
EDIT :
If you don’t want to create a class field for each form, you can do in this way:
Create a class dictionary field containing the open form for each form-type:
Then change the previous method to:
Now you can call it like this: