Joa.know
Joa.know

Reputation: 37

close windows form when users click outside

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.

Upvotes: 3

Views: 5256

Answers (2)

Ahmad
Ahmad

Reputation: 12737

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

Upvotes: 2

kush
kush

Reputation: 987

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

Winforms: Close modal dialog when clicking outside the dialog

Upvotes: 5

Related Questions