Friday, March 13, 2009

how to close a form when it loses focus vb.net

Put the the Me.Close call in the _Deactivate event.

The scenario is such:
- You have two forms, Form1 and Form2.  In Form1 you have a button and on the button_click event it just calls Form2.Show.  However when you click back on form1 you want Form2 to close.  You don't want it to go to the background, you want it out of there.  The solution is to close it in the DeActivate event

  Private Sub Form2_Deactivate(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Deactivate
    Me.Close()
  End Sub

No comments: