I have a form displayed in my app and what I want to do is detect when the user clicks outside of the form and close it, however I am not sure how to do this?
Thanks
Simon
Printable View
I have a form displayed in my app and what I want to do is detect when the user clicks outside of the form and close it, however I am not sure how to do this?
Thanks
Simon
I think you can get the form rectangle, and check if the click it's inside the rectangle, if not just close it...
Sample:
vb.net Code:
Public Class Form1 Private WithEvents tmr As New Timer Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load With tmr .Interval = 100 .Start() End With End Sub Private Sub tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmr.Tick Dim ptIni As Point = Me.PointToScreen(New Point(0, 0)) Dim ptTam As Point = Me.PointToScreen(New Point(Me.Size)) If MouseButtons = Windows.Forms.MouseButtons.Left Then Dim rec As New Rectangle(ptIni.X, ptIni.Y, ptTam.X, ptTam.Y) If Not rec.Contains(MousePosition) Then Me.Close() End If End If End Sub End Class