Hardware

Implement a MouseExit event for VB6 controls

The MouseMove event lets you detect when a mouse enters a VB6 control; however, Visual Basic doesn't have a built-in event that allows you to determine when a mouse exits a control. This tip explains how easy it is to create your own MouseExit event.

If you want to detect when the mouse enters a VB6 control, you can check via the MouseMove event. But how do you detect when the mouse exits a control? Visual Basic has no built-in event for this action, but it's easy to create your own.

First, put the control on a Frame control that is just slightly larger than the control on all sides. Set the caption of the Frame control to a blank string and set its BorderStyle property to 0-None.

Next, declare a form-level Boolean variable that is True when the mouse is over the control and False if it's not. For a button named Command1 this might look like the following (note: Booleans are False by default):

Dim MouseInCommand1 As Boolean

Now, write a sub that performs the actions that you want to occur when the mouse leaves the control:

Private Sub Command1_MouseExit()
...
End Sub

In the MouseMove event procedure for the control, set the Boolean variable to True:

Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, _
  X As Single, Y As Single)

    MouseInCommand1 = True

End Sub

Finally, in the MouseMove event procedure for the Frame, check the Boolean variable. If it is True, set it to False and call the procedure that you defined before:

Private Sub Frame1_MouseMove(Button As Integer, Shift As Integer, X As Single, 
_
    Y As Single)

If MouseInCommand1 Then
    MouseInCommand1 = False
    Call Command1_MouseExit
End If

End Sub

Now you've created your very own MouseExit event, which allows you to determine when a mouse exits a control.

Advance your scripting skills to the next level with TechRepublic's free Visual Basic newsletter, delivered each Friday. Automatically sign up today!

0 comments

Editor's Picks