Search code examples
vb.netformsmdi

Set MdiParent from a ShowDialog form


I've got a form, frmEmail which is called using f.ShowDialog (Where f is an instance of frmEmail).

How can I, when closing the form, open an instance of frmOrder but set fOrder.MdiParent = frmMain?

frmMain is my MDI form, frmOrder is a form I need to open, and frmEmail is the form I'm opening it from.

My code at the moment is

revoke = True

Dim f As New frmOrder(con, False, False, , orderNum)
f.MdiParent = ' Not sure what to put here?
f.Show()

Me.Close()

Do I need to pass in a variable that stores reference to frmMain? Is there another way to do it?

Obviously I can't use f.MdiParent = Me.MdiParent, because as I mentioned before, frmEmail is opened using f.ShowDialog so has no MdiParent.


Solution

  • You can still open a form in ShowDialog and set the "Owner" property.

    Where you're opening frmEmail, instead of opening it like using f.ShowDialog(), use f.ShowDialog(Me.MdiParent) (If your MDI form is the parent of the form where you open frmEmail, otherwise just tweak it as relevant).

    That way, when opening frmOrder, you set the MdiParent property to be frmEmail's owner property.

    Dim f As New frmOrder(con, False, False, , orderNum)
    f.MdiParent = Me.Owner
    f.Show()