MouseMove Event : When mouse is outside the edge or border of command button would like to change the background colour of commandbutton

SamDsouza

Board Regular
Joined
Apr 16, 2016
Messages
205
Hello
https://stackoverflow.com/questions/12200618/mousemove-what-is-the-reverse-event
Easier way: in your MouseMove event, test the X and Y arguments against the control's width and height (minus a margin, say 5) - if the mouse is in the margin, consider it a "Mouse out" and change the control's colours accordingly. No need for concurrent buttons, z-order manipulation, frames, etc.
How is above statement in RED justified with below code as i am stuck and color does not change when the mouse is outside the border or edge of command button
Code:
Private Sub CommandButton1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal y As Single)
 CommandButton1.ForeColor = RGB(191, 191, 191)
 CommandButton1.BackColor = RGB(31, 78, 120)


x = CommandButton1.Width - 5
y = CommandButton1.Height - 5


If x = x - 5 And y = y - 5 Then
   CommandButton1.BackColor = vbWhite
   CommandButton1.ForeColor = vbRed
End If


End Sub
FYI - somehow the thread 36613 of this forum went above my head


Thanks
SamD
 
Last edited:

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
How about something like this:
Code:
Private Sub CommandButton1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    Dim farXmargin As Single, farYmargin As Single
    Dim marginSize As Single
    
    marginSize = 5
    
    CommandButton1.ForeColor = RGB(191, 191, 191)
    CommandButton1.BackColor = RGB(31, 78, 120)
    
    farXmargin = CommandButton1.Width - marginSize
    farYmargin = CommandButton1.Height - marginSize
    
    If X < marginSize Or Y < marginSize Or X > farXmargin Or Y > farYmargin Then
        CommandButton1.BackColor = vbWhite
        CommandButton1.ForeColor = vbRed
    End If
End Sub

The variable X gives you the position from the left edge, and Y is from the top. Therefore, an X, Y coordinate of (0, 0) is the top left corner.

The code doesn't need to calculate the left or top margins since a value less than 5 (for example) for either one means that the cursor is within 5 units from the left or top edges. To see if the mouse is closer to the bottom or right edges, the code calculates the far margins for X and Y. Then if the X variable is < 5 or > farXmargin, it is considered to be the "MouseOut" as stated in your quote, and the color changes. Same for Y.

Note, however, that moving the mouse really fast out of the button from the center may not capture the margin, so it may not change. The resolution of the mouse coordinates isn't fine enough for a super-quick movement out of the button edges. Normal mousing might be okay, though.
 
Last edited:
Upvote 0
Excellent :)

The Perfect one:)

Thank you so much for your help and explanation. This indeed helped me understand for mousemove Event.
Thanks once more :beerchug:
SamD
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top