Results 1 to 4 of 4

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

  1. #1
    Board Regular
    Join Date
    Apr 2016
    Posts
    74
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

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

    Hello
    https://stackoverflow.com/questions/...-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 by SamDsouza; Oct 14th, 2019 at 09:58 AM.

  2. #2
    Board Regular
    Join Date
    Mar 2016
    Posts
    234
    Post Thanks / Like
    Mentioned
    3 Post(s)
    Tagged
    2 Thread(s)

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

    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 by shknbk2; Oct 14th, 2019 at 05:34 PM.
    Hobby: VBA and some .Net
    Career: the world of patent and trademark protection

    See
    Where do I paste the code that I find on the internet to implement any code I provide

    - forum use guidelines, forum rules and terms of use
    - Try searching for your answer first, see how
    - Read the FAQs
    - List of BB codes

  3. #3
    Board Regular
    Join Date
    Apr 2016
    Posts
    74
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

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

    Excellent

    The Perfect one

    Thank you so much for your help and explanation. This indeed helped me understand for mousemove Event.
    Thanks once more
    SamD
    Last edited by SamDsouza; Oct 17th, 2019 at 01:14 AM.

  4. #4
    Board Regular
    Join Date
    Mar 2016
    Posts
    234
    Post Thanks / Like
    Mentioned
    3 Post(s)
    Tagged
    2 Thread(s)

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

    You're very welcome.
    Hobby: VBA and some .Net
    Career: the world of patent and trademark protection

    See
    Where do I paste the code that I find on the internet to implement any code I provide

    - forum use guidelines, forum rules and terms of use
    - Try searching for your answer first, see how
    - Read the FAQs
    - List of BB codes

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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