title: Button that opens a form, or closes if open
body: <p>I'm new to VB.net and have a question I hope you can help me with.</p>
  
  <p>I have a form which has a button on and when i press the button form 2 opens. I would then like for the button on form 1 to then close form 2 if it is pressed while form 2 is open. </p>
  
  <p>I have the following code inside the button click sub..</p>
  
  <pre><code>Dim openForm As Form2
  openForm =New Form2()
  
  
  If Application.OpenForms. OfType(Of Form2).Any() Then
  
  Form2.Close()
  
  Else
  
  openForm.Show()
  openForm = Nothing
  
  End If
  </code></pre>
  
  <p>Form 2 opens when I press the button the first time. However pressing the button again does nothing, the form doesn't close.</p>
  
  <p>Any help will be much appreciated. </p>
  
  <p>Thanks</p>