Results 1 to 24 of 24

Thread: OnMouse over event?

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2006
    Posts
    55

    OnMouse over event?

    Hi guys,
    Just wondering if there is a onMouseover event for msaccess/vb? i want to display a comment (label) that is set to invisible initially, but when the user puts their mouse over a button (not clicking on it) then it displays the comment or label on the form. I have already tried the on got focus, and on mouse move but to no avail.
    Cheers
    GK

  2. #2

    Thread Starter
    Member
    Join Date
    Nov 2006
    Posts
    55

    Re: OnMouse over event?

    Sorry,
    The Onmouse over does work, but how do i make the label dissapear again if the user goes off the button again?
    Cheers

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2006
    Posts
    55

    Re: OnMouse over event?

    here is my current code:
    Private Sub Command2_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Label22.Visible = True
    End Sub

  4. #4

    Thread Starter
    Member
    Join Date
    Nov 2006
    Posts
    55

    Re: OnMouse over event?

    here is my current code:
    Code:
    Private Sub Command2_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Label22.Visible = True
    End Sub

  5. #5
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: OnMouse over event?

    Try this (in design mode set the visible property of the Label = False)
    VB Code:
    1. Private Declare Function SetCapture Lib "user32" (ByVal hWnd As Long) As Long
    2. Private Declare Function ReleaseCapture Lib "user32" () As Long
    3. Private Declare Function GetCapture Lib "user32" () As Long
    4.  
    5. Private Sub Command2_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    6.     If (X < 0 Or X > Command2.Width) Or (Y < 0 Or Y > Command2.Height) Then
    7.        Call ReleaseCapture
    8.         Label1.Visible = False
    9.     ElseIf Command2.hWnd <> GetCapture Then
    10.        Call SetCapture(Command2.hWnd)
    11.        Label1.Visible = True
    12.     End If
    13. End Sub

  6. #6

    Thread Starter
    Member
    Join Date
    Nov 2006
    Posts
    55

    Resolved Re: OnMouse over event?

    Cheers that works.
    I just realised you can use the Control Tip Text on the other tab in properties for a button. this will display a comment on mouse over for that button.
    Champinco

  7. #7
    Addicted Member
    Join Date
    Dec 2006
    Posts
    208

    Re: OnMouse over event?

    What is this label for is it something that will change. Or is it a set thing to say what the thing the user is hovering over does. If it is the second then you could use a tool tip. This would show a line of text when the user hovers over the button or whatever you want. It like when you hover over a button in word on the toolbar and it says what that is used for.
    Hope this is relevant
    Sam

    Sorry just realised you have written a reply while I was writing this

  8. #8
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: OnMouse over event?

    If you want a real solution (SetCapture and ReleaseCapture have their problems, it is available for one program and one control only at a time), take a look at SelfSub. It shows you the one and only way to do it right Of course, subclassing might feel like too much and too complex as itself, but it is actually only real Windows programming, it is a look into how all Windows programs really work.

  9. #9

    Thread Starter
    Member
    Join Date
    Nov 2006
    Posts
    55

    Re: OnMouse over event?

    Hi guys,
    Im getting problems with the code below...the error method or data member not found. in particular to hWnd variable. why is this so becuase this is declared in the 1st function SetCapture as (ByVal hWnd As Long) As Long.


    Code:
    Private Declare Function SetCapture Lib "user32" (ByVal hWnd As Long) As Long
    Private Declare Function ReleaseCapture Lib "user32" () As Long
    Private Declare Function GetCapture Lib "user32" () As Long
    
    Private Sub Command2_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If (X < 0 Or X > Command2.Width) Or (Y < 0 Or Y > Command2.Height) Then
           Call ReleaseCapture
            Label9.Visible = False
        ElseIf Command2.hWnd <> GetCapture Then
           Call SetCapture(Command2.hWnd)
           Label9.Visible = True
        End If
    End Sub
    Cheers
    Champinco

  10. #10
    Frenzied Member zynder's Avatar
    Join Date
    Nov 2006
    Location
    localhost
    Posts
    1,434

    Re: OnMouse over event?

    Try this without the API.

    VB Code:
    1. Private Sub Command2_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    2.     Label1.Visible = True
    3. End Sub
    4.  
    5. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    6.     Label1.Visible = False
    7. End Sub

    Hope that helps.

  11. #11
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: OnMouse over event?

    Quote Originally Posted by Champinco
    Hi guys,
    Im getting problems with the code below...the error method or data member not found. in particular to hWnd variable. why is this so becuase this is declared in the 1st function SetCapture as (ByVal hWnd As Long) As Long.
    you mentioned access at the start - is this VBA?

    if so then you won't be able to use SetCapture because the hWnds for UserForm controls are not exposed.

  12. #12

    Thread Starter
    Member
    Join Date
    Nov 2006
    Posts
    55

    Re: OnMouse over event?

    zynder,
    yes this works...but only that it shows the label when the mouse is over the button, but does not get rid of the label once the mouse is off the button again.

    bushmobile,
    yes this is VBA. so this wont work? what i want is a label displayed onmouseover for a button, and then label not displayed once the mouse is not on it again. i have multiple buttons on a form and wish to switch on and off different labels for the corresponding buttons. I dont want to use Control Tip text because the user has to scroll over the button and wait several seconds before a message (label) is displayed. I wish to have an instantaneous message (label) on mouse over for a button then dissapear after they are no "mousing over" longer on that button.

  13. #13
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: OnMouse over event?

    change the sub name in zynder's code to UserForm_MouseMove.

  14. #14

    Thread Starter
    Member
    Join Date
    Nov 2006
    Posts
    55

    Re: OnMouse over event?

    do you mean both subs or the first/second?
    also do i call it Command2_UserForm_MouseMove or just UserForm_MouseMove?
    i.e:
    Code:
    Private Sub Command2_UserForm_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
        Label9.Visible = True
    End Sub
    
    Private Sub Command2_UserForm_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
        Label9.Visible = False
    End Sub
    Cheers
    GK

  15. #15
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: OnMouse over event?

    leave Command2_MouseMove alone, just change Form_MouseMove to UserForm_MouseMove

  16. #16

    Thread Starter
    Member
    Join Date
    Nov 2006
    Posts
    55

    Re: OnMouse over event?

    this does not work: (yes it does show the label but once i move off the button the label is still displayed.

    Code:
    Private Sub Command2_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
        Label9.Visible = True
    End Sub
    
    Private Sub UserForm_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
        Label9.Visible = False
    End Sub

  17. #17
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: OnMouse over event?

    You'd have to add the Label9.Visible = False code to all other controls, or atleast all the controls that are near the button, ie. if you have the button in a frame, you have to add the code to the frame's MouseMove event procedure.

    Yeah, it isn't a very elegant solution, but since subclassing goes way ahead of your skill level, there really isn't many options...

  18. #18

    Thread Starter
    Member
    Join Date
    Nov 2006
    Posts
    55

    Re: OnMouse over event?

    ok...so going back to bushmobiles comment:
    if so then you won't be able to use SetCapture because the hWnds for UserForm controls are not exposed.
    then how would i work around this for my other code/post.


    Hi guys,
    Im getting problems with the code below...the error method or data member not found. in particular to hWnd variable. why is this so becuase this is declared in the 1st function SetCapture as (ByVal hWnd As Long) As Long.



    Code:
    Code:
    Private Declare Function SetCapture Lib "user32" (ByVal hWnd As Long) As Long
    Private Declare Function ReleaseCapture Lib "user32" () As Long
    Private Declare Function GetCapture Lib "user32" () As Long
    
    Private Sub Command2_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If (X < 0 Or X > Command2.Width) Or (Y < 0 Or Y > Command2.Height) Then
           Call ReleaseCapture
            Label9.Visible = False
        ElseIf Command2.hWnd <> GetCapture Then
           Call SetCapture(Command2.hWnd)
           Label9.Visible = True
        End If
    End Sub
    Cheers
    Champinco

  19. #19
    Frenzied Member zynder's Avatar
    Join Date
    Nov 2006
    Location
    localhost
    Posts
    1,434

    Re: OnMouse over event?

    Can you post a screen shot of your GUI?

  20. #20

    Thread Starter
    Member
    Join Date
    Nov 2006
    Posts
    55

    Re: OnMouse over event?

    as attached.
    there are different buttons, and i wish to simply have a mouse over event displaying a label once the user mouses over the button, and then also dissapears once they are off the button. similar to a control tip text event.
    I wish to have a different label/message for each button.
    cheers
    GK
    Attached Images Attached Images  

  21. #21
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: OnMouse over event?

    ok, here's a possible solution for you.

    you need to place the name of the label you want to be associated with the button in the button's Tag property. for example, I placed Label1 into the Tag property of CommandButton1.

    the code
    VB Code:
    1. Private Type POINTAPI
    2.     X As Long
    3.     Y As Long
    4. End Type
    5.  
    6. Private Declare Function GetCursorPos Lib "user32" ( _
    7.     lpPoint As POINTAPI) As Long
    8.    
    9. Private Declare Function ClientToScreen Lib "user32" ( _
    10.     ByVal hwnd As Long, lpPoint As POINTAPI) As Long
    11.    
    12. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
    13.     ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    14.  
    15. Private bRunning As Boolean
    16. Private lhWnd As Long
    17.  
    18. Private Sub UserForm_Initialize()
    19.     lhWnd = FindWindow(vbNullString, Me.Caption)
    20. End Sub
    21.  
    22. Private Sub UserForm_Activate()
    23.     If Not bRunning Then
    24.         bRunning = True
    25.         TimerLoop
    26.     End If
    27. End Sub
    28.  
    29. Private Sub TimerLoop()
    30.     Dim ptCursor As POINTAPI, ptClient As POINTAPI, ptBlank As POINTAPI
    31.     Dim lX As Long, lY As Long
    32.    
    33.     Do While bRunning
    34.         DoEvents
    35.        
    36.         GetCursorPos ptCursor
    37.         ClientToScreen lhWnd, ptClient
    38.        
    39.         lX = (ptCursor.X - ptClient.X) * 0.75
    40.         lY = (ptCursor.Y - ptClient.Y) * 0.75
    41.        
    42.         If lX > -1 And lY > -1 Then GenericMouseOver lX, lY
    43.        
    44.         ptClient = ptBlank
    45.     Loop
    46.     Unload Me
    47. End Sub
    48.  
    49. Private Sub GenericMouseOver(ByVal lX As Long, lY As Long)
    50.     Dim ctl As Control
    51.    
    52.     For Each ctl In Me.Controls
    53.         If TypeOf ctl Is CommandButton Then
    54.             If lX > ctl.Left And lX < (ctl.Left + ctl.Width) And _
    55.                lY > ctl.Top And lY < (ctl.Top + ctl.Height) Then
    56.                 If Not Me.Controls(ctl.Tag).Visible Then Me.Controls(ctl.Tag).Visible = True
    57.             Else
    58.                 If Me.Controls(ctl.Tag).Visible Then Me.Controls(ctl.Tag).Visible = False
    59.             End If
    60.         End If
    61.     Next ctl
    62. End Sub
    63.  
    64. Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    65.     If bRunning Then
    66.         Cancel = True
    67.         bRunning = False
    68.     End If
    69. End Sub
    the code works - it's just a question of whether you'll be able to integrate it correctly.

  22. #22
    New Member
    Join Date
    Nov 2007
    Posts
    1

    Thumbs up Re: OnMouse over event?

    Not sure if anyone is still checking this, but here is what I use for my form mousemove VBA. It's not nearly as complex as some of the other postings, but it works for me.

    When the form opens I make the label invisible. If the user moves the mouse over the command button, then the button text is bolded, the text color changes, and the label is set to visible. If the user moves the mouse over the detail section of the form, then the label is set back to invisible and the button text is set back to normal.

    -----------------------------------------------------------------------
    Option Compare Database
    Option Explicit
    'Created By Jeff McCoy

    Private Sub Command0_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    On Error GoTo Err_Command0
    With Me.Controls("Command0")
    .ForeColor = 16711680
    .FontWeight = 700
    End With
    Me.txt1.Visible = True
    Exit_Command0:
    Exit Sub
    Err_Command0:
    MsgBox Err.Description
    Resume Exit_Command0
    End Sub

    Private Sub Detail_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    On Error GoTo Err_Detail
    With Me.Controls("Command0")
    .ForeColor = 0
    .FontWeight = 400
    End With
    Me.txt1.Visible = False
    Exit_Detail:
    Exit Sub
    Err_Detail:
    MsgBox Err.Description
    Resume Exit_Detail
    End Sub

    Private Sub Form_Open(Cancel As Integer)
    On Error GoTo Err_Form_Open
    Me.txt1.Visible = False
    Exit_Form_Open:
    Exit Sub
    Err_Form_Open:
    MsgBox Err.Description
    Resume Exit_Form_Open
    End Sub

  23. #23
    New Member
    Join Date
    Dec 2011
    Posts
    4

    Re: OnMouse over event?

    Quote Originally Posted by JBM18 View Post
    Not sure if anyone is still checking this, but here is what I use for my form mousemove VBA. It's not nearly as complex as some of the other postings, but it works for me.

    When the form opens I make the label invisible. If the user moves the mouse over the command button, then the button text is bolded, the text color changes, and the label is set to visible. If the user moves the mouse over the detail section of the form, then the label is set back to invisible and the button text is set back to normal.
    This method does not work reliability. If you hover over the command button and then move the mouse swiftly off the detail section of the form to another area of the screen the command button will remain with the hover attributes. This is because the move move events are not fired for the detail section because the mouse was not in the detail section long enough. This solution works as long as you only slowly move the mouse around. The proper solution is to use Windows events to detect the hover. Unfortunately this is also more complex. See http://www.codeproject.com/Articles/...-to-VB-Control for an implementation.

  24. #24
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,207

    Re: OnMouse over event?

    Quote Originally Posted by jeakins01 View Post
    The proper solution is to use Windows events to detect the hover. Unfortunately this is also more complex.
    Using a VB.TimerCtl which destroys itself (when nothing is hovered),
    one can avoid TrackMouseEvent and the SubClassing-approach.

    Code for a Form (create two PictureBoxes on it):

    Code:
    Option Explicit
    
    Private Declare Function GetCursorPos Lib "user32" (pPoint As Any) As Long
    Private Declare Function WindowFromPoint Lib "user32" (ByVal X&, ByVal Y&) As Long
    
    Private ctlHov As Object, WithEvents tmrHov As VB.Timer
     
    Private Sub MouseOver(Sender As Object)
      If TypeOf Sender Is PictureBox Then Sender.BackColor = vbGreen
      Debug.Print Sender.Name, "MouseOver"
    End Sub
    Private Sub MouseOut(Sender As Object)
      If TypeOf Sender Is PictureBox Then Sender.BackColor = Me.BackColor
      Debug.Print Sender.Name, "MouseOut"
    End Sub
     
    Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
      SetHoverWatch Picture1
    End Sub
    Private Sub Picture2_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
      SetHoverWatch Picture2
    End Sub
    
    Private Sub SetHoverWatch(ControlUnderMouse As Object)
      If tmrHov Is Nothing Then Set tmrHov = Controls.Add("VB.Timer", "tmrHov")
         tmrHov.Interval = 50
      If Not ctlHov Is ControlUnderMouse Then
        If Not ctlHov Is Nothing Then MouseOut ctlHov
        MouseOver ControlUnderMouse
      End If
      Set ctlHov = ControlUnderMouse
    End Sub
     
    Private Sub tmrHov_Timer()
      Dim Pt(0 To 1) As Long: GetCursorPos Pt(0)
      On Error GoTo 1
        If ctlHov.hWnd = WindowFromPoint(Pt(0), Pt(1)) Then Exit Sub
    1 If Not ctlHov Is Nothing Then MouseOut ctlHov: Set ctlHov = Nothing
      Controls.Remove "tmrHov": Set tmrHov = Nothing
    End Sub
    HTH

    Olaf

Posting Permissions

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



Click Here to Expand Forum to Full Width