title: Disposing a hidden form
body: <p>I have a question about closing and disposing a hidden child form.</p>
  
  <p>Parent form with two buttons:</p>
  
  <pre><code>Public Class Form1
      Dim F2 As Form2
  
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
          F2 = New Form2
      End Sub
  
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
          F2.Show()
      End Sub
  
      Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
          F2.Hide()
      End Sub
  End Class
  </code></pre>
  
  <p>Child form:</p>
  
  <pre><code>Public Class Form2
      Private Sub Form2_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
              e.Cancel = True
              Me.Hide()
      End Sub
  
      Private Sub Form2_VisibleChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.VisibleChanged
          MsgBox("Form2.Visible = " &amp; Me.Visible.ToString)
      End Sub
  
      Private Sub Form2_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
          MsgBox("Form2 has been disposed.")
      End Sub
  End Class
  </code></pre>
  
  <p>As long as Form1 is open, I don't want to close Form2. So that part works.<br>
  But I do want to close Form2 when Form1 is closed. Do I have to close it explicitly from Form1? And add more logic to Form2_FormClosing()?<br>
  As is, Form2_Disposed() never gets called as far as I can tell (I never get the message box). Is this correct?<br>
  When Form1 is disposed, variable F2 no longer exists. Will the Form2 be disposed by the garbage collector later on or not?</p>