3

anybody have an idea how to write a code if the user clicks outside windows form, the form will automatically close? I.e., I had two forms, when i show form2 and then i click outside of it, form 2 will closed.

kush
  • 979
  • 1
  • 15
  • 32
Joa.know
  • 37
  • 1
  • 5
  • 10

2 Answers2

5

Does this help? Try the first two solutions, one of which should work for you.

Winforms: Close modal dialog when clicking outside the dialog

Community
  • 1
  • 1
kush
  • 979
  • 1
  • 15
  • 32
2

You can simply utilize the LostFocus built-in event of the Form like this

Private Sub Form1_LostFocus(ByVal sender As Object, ByVal e As EventArgs) _
        Handles Me.LostFocus

    Me.Close()
End Sub
Ahmad
  • 12,336
  • 6
  • 48
  • 88