1
votes

I have a Main Form which is a MDI container, then another form (Lets call it Sub Form) which opens within Main form container. However, I want another form (Sub Form child) to open when I press a certain button on the sub form and it to exist in the Main form container space, but I would also like it to be a child of the sub form, so if I close the sub form, it would also close 'Sub Form Child'

I tried setting the 'Sub Form Child' parent to 'Sub Form' and its .MdiParent to Main form but It would not let me.

How would I achieve this?

1

1 Answers

1
votes

Just keep a reference to the "Sub Form Child" when you create it, and then close it when the FormClosing() event of the "Sub Form" fires:

Public Class SubForm

    Private _SubFormChild As SubFormChild = Nothing

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        If IsNothing(_SubFormChild) OrElse _SubFormChild.IsDisposed Then
            _SubFormChild = New SubFormChild
            _SubFormChild.MdiParent = Me.MdiParent
            _SubFormChild.Show()
        Else
            If _SubFormChild.WindowState = FormWindowState.Minimized Then
                _SubFormChild.WindowState = FormWindowState.Normal
            End If
            _SubFormChild.Activate()
        End If
    End Sub

    Private Sub SubForm_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If Not IsNothing(_SubFormChild) AndAlso Not _SubFormChild.IsDisposed Then
            _SubFormChild.Close()
        End If
    End Sub

End Class