Click here to Skip to main content
15,901,373 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wrote code control box handling for Form1 in VB.Net using mouse hover and mouse leave events, but it is not working. Can anyone tell what the mistake is?

What I have tried:

Private Sub Form1_MouseLeave(sender As Object, e As EventArgs) Handles MyBase.MouseLeave
        Me.ControlBox = False
    End Sub

    Private Sub Form1_MouseHover(sender As Object, e As EventArgs) Handles MyBase.MouseHover
        Me.ControlBox = True
    End Sub
Posted
Comments
Jo_vb.net 23-Dec-23 6:36am    
I think you should not set the Control to False or True, this is not really helpful.

Instead set a property (Enabled or Visible) of the control to False or True like:

Me.ControlBox.Enabled = False

1 solution

For me this code is working well.
I suppose that you have also several Controls on your Form. If the Mouse hovers over one of this it leaves the Form.
If you want that they also make your Controlbox visible you have to add each Hover-Event from this Controls also to the Form1_MouseHover-Method.
Perhaps you should also look if the Form1_MouseLeave really detect the Leave-Event.
I suggest that you create a method which "knows" that the Mouse is over the Form or one of their controls ...

Here is a easy-made code-sample which works with a Timer which belongs to the Form :
VB
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
     Dim p As Point = PointToClient(MousePosition)
      If (p.X >= 0) And (p.X < Width) And (p.Y >= 0) And (p.Y < Height) Then CheckBox1.Checked = True Else CheckBox1.Checked = False
 End Sub

I gave the Timer a small Interval (100 ms) - the only thing you should know is that the Form-Header and the Borders don't belong to the Size-Definition from the Form. The Size of them depends on the selected FormBorderStyle. With this you have to calculate also ...

and another one :
VB
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
    Dim p As Point = MousePosition
    Dim r As Rectangle = Me.ClientRectangle
    r.X += Location.X
    r.Y += Location.Y
    If (p.X >= r.Left) And (p.X <= r.Right) And (p.Y >= r.Top) And (p.Y <= r.Bottom) Then CheckBox1.Checked = True Else CheckBox1.Checked = False
End Sub
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900