Leron
Leron

Reputation: 9876

Windows Forms - how to get the MDIparent Form

I have a MDIform application, where a form named MainForm is set to parent. Every time I open a new form I'm closing the others and I do it with custom method that gets 3 arguments one of which is object representing the MDIparent. Mostly I open forms from my MainForm which is also a MDIparent but I have this situation where I have to open a child form form another child form. Let's say that if I call my method for opening a new form like :

NewForm newForm = getMyForm(this, arg2, arg3)

when I'm in the MainForm which is also a parent it's easy. But how to call my method or how to pass the object which is my MDIparent when I'm calling from other child form?

Upvotes: 2

Views: 10080

Answers (2)

spajce
spajce

Reputation: 7092

Have you tried like

From your MainForm

 var f = new NewForm() { MdiParent = this};
 f.Show();

as example of Mr. Habib, Take note that the method from the MainForm is must be a public.

MainForm frm = this.MdiParent as MainForm;
if(frm != null)
{
  frm.Methods(this, arg2, arg3)
}

Upvotes: 1

Habib
Habib

Reputation: 223322

You can use Form.MdiParent property which will give you the MDI Parent for the current form.

MainForm frm = this.MdiParent as MainForm;
if(frm != null)
    //form found

Upvotes: 5

Related Questions