mousemove event+tooltip over commandbutton

arnie_83

New Member
Joined
Nov 2, 2006
Messages
43
hi
i am trying to pop up a tooltip over a commandbutton in excel VBA. i have used a label which contained the tooltip text .while mousemove over the button,the label visibility is set to true. But the label doesnot disappear after the mouse is taken away from the commandbutton. kindly help

with thanks
Arnie
 
Hello arnie_83, welcome to the board.
You need some kind of a frame or something around the commandbutton and then use the same mouse move code for the frame that you used for the commandbutton, only setting the visible property to false.
For example, you mouse move over the button, the code is executed to make the label visible = True.
You mouse move off the button, (you are now moving onto the frame, right?), the same type of code is now executed to make the label visible = False.
(Know what I mean?)
 
Upvote 0
me too
not sure about method to handle this case.
with solution create frame
Code:
Frame area to hide on mousemove
   -------------------------------------
   |Button to show on mousemove|
   -------------------------------------
sometime mouse fastly move pass the Frame.
By the frame can't detect(may be that frame little area)

could you share another ways for resolve this...
 
Upvote 0
To do this, put the following code in a standard code module (making it
available to the whole workbook).

Code:
'------------- bas module ------------------------
Option Explicit

Declare Function GetSystemMetrics Lib "user32" ( _
    ByVal nIndex As Long) As Long

Declare Function GetSysColor Lib "user32" ( _
    ByVal nIndex As Long) As Long


Public Function CreateToolTipLabel(objHostOLE As Object, _
                                   sTTLText As String) As Boolean
Dim objToolTipLbl As OLEObject
Dim objOLE As OLEObject

Const SM_CXSCREEN = 0
Const COLOR_INFOTEXT = 23
Const COLOR_INFOBK = 24
Const COLOR_WINDOWFRAME = 6

  Application.ScreenUpdating = False   'just while label is created and formatted

  For Each objOLE In ActiveSheet.OLEObjects
      If objOLE.Name = "TTL" Then objOLE.Delete 'only one can exist at a time
  Next objOLE

 'create a label control...
  Set objToolTipLbl = ActiveSheet.OLEObjects.Add(ClassType:="Forms.Label.1")

 '...and format it to look as a ToolTipWindow
  With objToolTipLbl
      .Top = objHostOLE.Top + objHostOLE.Height - 10
      .Left = objHostOLE.Left + objHostOLE.Width - 10
      .Object.Caption = sTTLText
      .Object.Font.Size = 8
      .Object.BackColor = GetSysColor(COLOR_INFOBK)
      .Object.BackStyle = 1
      .Object.BorderColor = GetSysColor(COLOR_WINDOWFRAME)
      .Object.BorderStyle = 1
      .Object.ForeColor = GetSysColor(COLOR_INFOTEXT)
      .Object.TextAlign = 1
      .Object.AutoSize = False
      .Width = GetSystemMetrics(SM_CXSCREEN)
      .Object.AutoSize = True
      .Width = .Width + 2
      .Height = .Height + 2
      .Name = "TTL"
  End With
  DoEvents
  Application.ScreenUpdating = True

 'delete the tooltip window after 5 secs
  Application.OnTime Now() + TimeValue("00:00:05"), "DeleteToolTipLabels"

End Function

Public Sub DeleteToolTipLabels()
Dim objToolTipLbl As OLEObject
  For Each objToolTipLbl In ActiveSheet.OLEObjects
      If objToolTipLbl.Name = "TTL" Then objToolTipLbl.Delete
  Next objToolTipLbl
End Sub


'------------end of bas module -------------

Then in the code module for the sheet that has the control, add some
mousedown event code. To get to this module, right-click on the sheet name
tab, and selecw code (or double-click on the sheet name from within the VB
IDE). Here is an example of how to call it, assuming that the command button
is calle cmdTooltipTest

Code:
Private Sub CmdTooltipTest_MouseMove(ByVal Button As Integer, _
                               ByVal Shift As Integer, _
                               ByVal X As Single, _
                               ByVal Y As Single)
Dim objTTL As OLEObject
Dim fTTL As Boolean

  For Each objTTL In ActiveSheet.OLEObjects
      fTTL = objTTL.Name = "TTL"
  Next objTTL

  If Not fTTL Then
     CreateToolTipLabel cmdTooltipTest, "ToolTip Label"
   End If

End Sub
 
Upvote 0
Hi,

perhaps this code is not as "powerful" as other systems, but wouldn't this not be simple and "enough" for the purpose ?

Code:
Private Sub CommandButton1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
'to avoid calling "HideLabel1" multiple times
If Me.Label1.Visible = True Then Exit Sub
Me.Label1.Visible = True
Application.OnTime Now + TimeValue("00:00:03"), "Sheet1.HideLabel1"
End Sub

Private Sub HideLabel1()
Me.Label1.Visible = False
End Sub
change the "3" to the number of seconds you want the label to be visible
replace "Sheet1" by the codename of your sheet

kind regards,
Erik
 
Upvote 0
Erik -- your code does exactly what I want to do. However, there's a snag -- if I click on the button with which the tooltip is associated, MouseMove is invoked and I'm getting a run-time error on the visibility check. I do have a _Click method defined to run the macro I want, but that doesn't seem to be executing.
 
Upvote 0

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