0

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            
    }
kashif
  • 3,713
  • 8
  • 32
  • 47

2 Answers2

2

You could make the method generic, e.g. :

public void CreateMdiChildOrActivate<T>(ref T instance) where T : Form, new()
{
    if (instance == null || instance.IsDisposed)
    {
        instance = new T();
        instance.MdiParent = this;
        instance.Show();
    }
    else
    {
        instance.Activate();
    }
}

Usage:

private void studentsToolStripMenuItem1_Click(object sender, EventArgs e)
{
    CreateMdiChildOrActivate(ref this.stu);
}

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:

private Dictionary<Type,Form> openForms = new Dictionary<Type,Form>();

Then change the previous method to:

public void CreateMdiChildOrActivate<T>() where T : Form, new()
{
    Form instance;
    openForms.TryGetValue(typeof(T), out instance);
    if (instance == null || instance.IsDisposed)
    {
        instance = new T();
        openForms[typeof(T)] = instance;
        instance.MdiParent = this;
        instance.Show();
    }
    else
    {
        instance.Activate();
    }
}

Now you can call it like this:

private void studentsToolStripMenuItem1_Click(object sender, EventArgs e)
{
    CreateMdiChildOrActivate<Student>();
}
digEmAll
  • 56,430
  • 9
  • 115
  • 140
  • @kashif: sorry I didn't understand... you want to call this method without passing any parameters ? – digEmAll Mar 26 '12 at 20:39
  • @kashif: if I understand you correctly you don't want to create a class field called Stu, but let the thing to the method, by passing just the type of the form to it, right ? Check my edit, I hope is what you want... – digEmAll Mar 27 '12 at 08:03
  • @kashif: They're both good, but the 2nd one is probably better if you have more than 3-4 forms to open because you don't need to have a lot of fields in the form. On the other hand, the second case allows only one instance of form for each form-type (e.g. just one Student form possible, not more...) but I think that's what you want... – digEmAll Mar 27 '12 at 10:04
  • what about my new Answer?? – kashif Feb 19 '13 at 20:53
  • I asked you this question in March. at that time I was not aware of Generics. Now I have a little knowledge. I think iterating through the mdi parant to look for the instance you are passing for and then if the instance is found just activating it else instantiating and then telling what is its MdiParant and then showing it is better then storing all the forms in Dictionary. digEmAll can you plz tell what does where do in generics?? – kashif Feb 20 '13 at 09:08
  • To be honest, I didn't understand your comment... however I suggest you to open another question, in this way you can explain what's your problem, what do you want to achieve posting code samples etc... – digEmAll Feb 25 '13 at 08:28
0
public void CreateMdiChild<T>(Form f) where T : Form, new()
{
    foreach (Form frm in f.MdiChildren)
    {
        if (frm.GetType() == typeof(T))
        {                        
            if (frm.WindowState == FormWindowState.Minimized)
            {
                frm.WindowState = FormWindowState.Normal;
            }
            else
            {
                frm.Activate();
            }
            return;
        }                    
    }
    T t = new T();
    t.MdiParent = f;
    t.Show();
}

Usage

CreateMdiChild<MyForm>()
kashif
  • 3,713
  • 8
  • 32
  • 47