vb.netwinformsmdimdiparent

Opening Mdi Parent as an Object won't let open Mdi Child form from another Mdi Child Form in VB.Net


I got a weird problem with MDI Parent form.
I open forms like -> Login Form(is not a MdiContainer) -> MdiParent (Is a MdiContainer).
But I wanted to pass a boolean value from Login Form to MdiParent Form so I declared a Friend variable in MdiParent Form and called MdiParent Object in Login Form as shown in below code.

Dim frm As New MdiParent
'frm.NormalMode = True
frm.Show()

Everything works fine. MDIParent form opened as expected.
But I have some child form in MdiParent form.
A child Form has a button which does is open another child form with Parent MDIParent shown below.

Dim frm1 As New Child2
frm1.MdiParent = MdiParent
frm1.anyvariable = value
frm1.Show()

But now it won't open. Like on button click it comes to the breakpoint passes through frm1.show() command line but it won't open.

Before I was Opening MDIParent directly like MdiParent.show() and everything worked fine.
All child Forms opened properly with this code.

Something Extra:
Also, When I run with MdiParent.Show() and Pause the code in VS2017 and uncomment the MDIParent code with Object (the one above) and comment MdiParent.Show() It works fine. Again, stopping and replaying the code creates issues.
Also, I didn't use that Boolean variable in MdiParent Form yet so it's not a problem I guess.
Using a Public variable is a convenient way, for now, I guess.
Also declaring Friend variables between two child forms is not an issue at all.


Solution

  • I think what's happening is that, in this line:

    frm1.MdiParent = MdiParent
    

    the part on the right is being interpreted as being the default instance of the MdiParent class rather than the MdiParent property of the current form. As a result, the new form is being parented by that default instance, which you haven't displayed, rather than the instance that you explicitly created and did display. That would also explain why it works when you use this:

    MdiParent.Show()
    

    which is displaying the default instance. The fix is easy. You just need to qualify the name to indicate that it is actually the property of the current form that you're referring to:

    frm1.MdiParent = Me.MdiParent
    

    Alternatively, use a better name for your form than MdiParent, like MainForm or the like. Then there will be no name clash.