Results 1 to 2 of 2

Thread: Cursor click outside form boundary

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Posts
    170

    Cursor click outside form boundary

    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

  2. #2
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: Cursor click outside form boundary

    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:
    1. Public Class Form1
    2.     Private WithEvents tmr As New Timer
    3.     Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    4.         With tmr
    5.             .Interval = 100
    6.             .Start()
    7.         End With
    8.     End Sub
    9.  
    10.     Private Sub tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmr.Tick
    11.         Dim ptIni As Point = Me.PointToScreen(New Point(0, 0))
    12.         Dim ptTam As Point = Me.PointToScreen(New Point(Me.Size))
    13.         If MouseButtons = Windows.Forms.MouseButtons.Left Then
    14.             Dim rec As New Rectangle(ptIni.X, ptIni.Y, ptTam.X, ptTam.Y)
    15.             If Not rec.Contains(MousePosition) Then
    16.                 Me.Close()
    17.             End If
    18.         End If
    19.     End Sub
    20. End Class

    Rate People That Helped You
    Mark Thread Resolved When Resolved

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width