Mouse hover

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tigger
    New Member
    • May 2007
    • 37

    Mouse hover

    Hi
    I have a label called buttonLabel. How do i display the label only when the mouse is over the button? I tried using mouse hover event and use a timer. But the label will disappear after 1 sec. But i want the label to disappear only when the mouse is not over the button. Is there a better way to do it?
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    Originally posted by tigger
    Hi
    I have a label called buttonLabel. How do i display the label only when the mouse is over the button? I tried using mouse hover event and use a timer. But the label will disappear after 1 sec. But i want the label to disappear only when the mouse is not over the button. Is there a better way to do it?
    How about clearing a counter in the button's MouseMove event. Set the timer interval fairly short, have it increment the counter, and hide the button when the counter passes some threshold.

    Um... or perhaps you could set a "hey, we've left the button" flag when the mouse moves over the parent form. Or even just set the button's ToolTipText property to the value you want to show.

    Comment

    • Sedecrem
      New Member
      • Aug 2007
      • 4

      #3
      I'm not sure if I understand your program but I will try to describe what I am thinking of.

      Does the program work as follows:

      There is an invisible label that becomes visible when the mouse is over a certain object and then becomes invisible when the mouse moves away.

      If so you can do this. Still have your timer, set the interval to 1. Rather than the timer simply turning the visibility off make it first check where the pointer is and relate it to where the object is eg if the pointer is located at x and y then you check whether
      x >= the left of the object
      x <= the left + the width of the object
      y >= the top of the object
      y <= the top + the height of the object

      If all these are true then the mouse is over the object so let label.Visible = True else if they are not label.Visible = False.

      [CODE=vb]If x >= cmd1.Left And x <= cmd1.Left + cmd1.Width And y >= cmd1.Top And y <= cmd1.Top + cmd1.Height Then
      lbl1.Visible = True
      Else
      lbl1.Visible = False
      End If[/CODE]

      Another more complicated version is shown here http://www.vb-helper.com/howto_message_over_button.html. They change the message depending on the where the mouse is rather than making a label invisible but its basically he same thing. Also they dynamically determine the controls position which you do not have to do if your form is static.

      Sedecrem
      Last edited by Killer42; Aug 21 '07, 10:27 PM. Reason: Tidied up a bit.

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Thanks for that, Sedecrem. Nice piece of work.

        I do have a question and a suggestion:
        • How do you plan to deal with the MouseMove event being captured by other controls?
          Or are you just relying on the label being hidden as the user leaves the control in question, before they hit another one? (Which is more or less what I had in mind with the flag-setting)
          Or did you have another idea in mind (API call, perhaps?) for detecting the mouse position?
        • Just as a general efficiency thing, I'd suggest that in a case like this, any values required to be checked a thousand times a second should be calculated once and placed in a variable. So for instance, the cmd1.Left + cmd1.Width would most likely be calculated in the Form_Load event or a ReSize event, and stored in a form-level variable. Same for the other calculated values. According to doctrine, it would also be worth moving the Left, Top, Width and Height properties into variables, as they are faster to access than control properties.

        Comment

        Working...