0

I want to detect if a user leaves the Form.
The following code works well but it fires off even when I'm still over the ControlBox of the Form.
What I thus want (was expecting) is that the the ControlBox also made up part and only after the mouse has left the Form and the adjoining ControlBox that the code would fire.

Private Sub Form1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.MouseLeave
    MsgBox("Im outside of form")
End Sub
Jimi
  • 29,621
  • 8
  • 43
  • 61
LabRat
  • 1,996
  • 11
  • 56
  • 91
  • The title bar and border of the form are the non-client area, so under the control of the OS. Your mouse events only relate to the client area. You would have to look into using unmanaged code to interact with the non-client area. – jmcilhinney Sep 17 '21 at 10:20
  • You can override WndProc or implement `IMessageFilter` in your Form, trap `WM_NCMOUSELEAVE` and verify whether `[Form].Bounds` contains `Cursor.Position`. – Jimi Sep 17 '21 at 16:38
  • MouseLeave is in general [unreliable](https://stackoverflow.com/a/2015186/17034). If the form doesn't have any child controls then you'd favor the Capture property. Setting it to True ensures you'll get a MouseMove event even if the mouse is moved beyond the border. – Hans Passant Sep 17 '21 at 18:16
  • Hans that is an interesting one I didn't know about that option. Anyhow I have fixed the problem with a very simple solution in the end. The problem was that a toolstrip on another from kept losing focus and I would have to press a button twice for it to fire. the first to put the form into focus the second to click the actual button I simply added a code that when the toolstrip (where the button resides) gets mouse enter event trigger the focus on the toolstrip... if you follow what I mean. Often in the end tons of complicated code is fixed with a few simple lines or changes in layout design – LabRat Sep 18 '21 at 06:04

0 Answers0