I am working with an MDI application.
I have three forms where in Form1 is MDIParent form.From this MDIParent form
another form FOrm2 is getting open which remains within the bounds if mdiparent
window.Form2 has a button, when clicked opens Form3 which I want it to get
open as child of MDIParent form(Form1).it does not remain within the bounds of
mdiparent form.How do I do this.
Form2 has a code:
Button1_Click ()
{
Form3 f3=new Form3();
f3.MDIParent=Form1;//error here
f3.show();
}

Dani AI

Generated

Short answer: make the new window a child of the actual MDI container instance (the Form1 object that has IsMdiContainer = true), not the Form class name or the current child form.

: the common mistakes seen here are already in the replies — trying to assign a class name instead of an instance, or assigning the new form to the current child (which is not an MDI container). was pointing in the right direction but used the wrong reference, and suggested using this as the parent (that will fail unless this is the MDI container). The correct approach is to use the MDI container instance (available as the MdiParent property when code runs inside a child) or to explicitly pass the container reference into the child when you create it.

A robust pattern is to pass the MDI parent into Form2 when you create Form2 and then use that stored reference when opening Form3. For example:

/* In the MDI parent when creating Form2 */
var f2 = new Form2(this);

/* In Form2, keep the passed parent and use it to create Form3 */
var child = new Form3();
child.MdiParent = _mdiParentReference;
child.Show();

Checklist / troubleshooting

  • Ensure the container form has IsMdiContainer = true.
  • Set the child’s MdiParent to the MDI container instance before calling Show().
  • If MdiParent is null inside Form2, either pass the parent in the constructor or find it via Application.OpenForms (careful with multiple instances).
  • Expect an InvalidOperationException if you try to set MdiParent to a form that is not an MDI container.

Microsoft docs: see the Form.MdiParent property for behavior and exceptions (Form.MdiParent).

Recommended Answers

All 2 Replies

Try

f3.MdiParent=Form1.MdiParent;

Try this,

Form3 frm = new Form3();
            frm.MdiParent = this;
            this.ActivateMdiChild(frm);         
            frm.Show();

Hope this will help you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.