Simulating a ToolTip Control (Class using a Standard label)!

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
9,621
Office Version
  1. 2016
Platform
  1. Windows
Hi there ,

Thought this may be of interest .

In response to a previous question,(see here : http://www.mrexcel.com/board2/viewtopic.php?t=214194&highlight=) I managed to put together this reusable custom tooltip Class which uses nothing but a simple label control. However, with a few tricks and in keeping with object oriented prgramming via wrapping the label functionality in a class module the customised label can now be easily added to one's programs as a propper object with its ownd Poperties & Methods thanks to the VBE Intelsense.

As opposed to real ToolTip Controls, you can set so many properties to this class namely : formatting Font, Size, colors...

I have tested this class and it worked for all standard controls apart from the ChekBox Control which doesn't seem to respond for some strange reason !

Anyway, here is a workbook download : http://www.savefile.com/files/3479241


Code in a Class Module named : CustomToolTipClss

Code:
Option Explicit

Public object As MSForms.Label
Public WithEvents form As MSForms.UserForm
Private WithEvents AnchorTxtCtrl As MSForms.TextBox
Private WithEvents AnchorLblCtrl As MSForms.Label
Private WithEvents AnchorChkCtrl As MSForms.CheckBox
Private WithEvents AnchorCmbCtrl As MSForms.ComboBox
Private WithEvents AnchorLstCtrl As MSForms.ListBox
Private WithEvents AnchorCmdCtrl As MSForms.CommandButton
Private WithEvents AnchorImgCtrl As MSForms.Image
Private WithEvents AnchorOptCtrl As MSForms.OptionButton
Private WithEvents AnchorTglCtrl As MSForms.ToggleButton
Private lbl As MSForms.Label
Private blnNotFirstEntry As Boolean


Private Sub AnchorChkCtrl_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    Call HideToolTip
End Sub

Private Sub AnchorChkCtrl_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    Call HideToolTip
End Sub

Private Sub AnchorChkCtrl_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    Call GenericMouseMove(AnchorChkCtrl, X, Y)
End Sub

Private Sub AnchorCmbCtrl_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    Call HideToolTip
End Sub

Private Sub AnchorCmbCtrl_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    Call HideToolTip
End Sub

Private Sub AnchorCmbCtrl_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    Call GenericMouseMove(AnchorCmbCtrl, X, Y)
End Sub

Private Sub AnchorCmdCtrl_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    Call HideToolTip
End Sub

Private Sub AnchorCmdCtrl_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    Call HideToolTip
End Sub

Private Sub AnchorCmdCtrl_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    Call GenericMouseMove(AnchorCmdCtrl, X, Y)
End Sub

Private Sub AnchorImgCtrl_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    Call HideToolTip
End Sub

Private Sub AnchorImgCtrl_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    Call GenericMouseMove(AnchorImgCtrl, X, Y)
End Sub

Private Sub AnchorLblCtrl_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    Call HideToolTip
End Sub

Private Sub AnchorLblCtrl_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    Call GenericMouseMove(AnchorLblCtrl, X, Y)
End Sub

Private Sub AnchorLstCtrl_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    Call HideToolTip
End Sub

Private Sub AnchorLstCtrl_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    Call HideToolTip
End Sub

Private Sub AnchorLstCtrl_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    Call GenericMouseMove(AnchorLstCtrl, X, Y)
End Sub


Private Sub AnchorOptCtrl_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    Call HideToolTip
End Sub

Private Sub AnchorOptCtrl_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    Call HideToolTip
End Sub

Private Sub AnchorOptCtrl_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    Call GenericMouseMove(AnchorOptCtrl, X, Y)
End Sub

Private Sub AnchorTglCtrl_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    Call HideToolTip
End Sub

Private Sub AnchorTglCtrl_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    Call HideToolTip
End Sub

Private Sub AnchorTglCtrl_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    Call GenericMouseMove(AnchorTglCtrl, X, Y)
End Sub

Private Sub AnchorTxtCtrl_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    Call HideToolTip
End Sub

Private Sub AnchorTxtCtrl_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    Call HideToolTip
End Sub

Private Sub AnchorTxtCtrl_MouseMove _
(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    Call GenericMouseMove(AnchorTxtCtrl, X, Y)
End Sub

Private Sub Class_Initialize()

    Set lbl = VBA.UserForms(0).Controls.Add("Forms.Label.1", "TipText")
    lbl.Tag = "ToolTip"
    lbl.Visible = False
    Set object = lbl

End Sub

Public Sub AttachTo(Ctrl As Object)

    Select Case TypeName(Ctrl)
        Case Is = "TextBox"
            Set AnchorTxtCtrl = Ctrl
        Case Is = "Label"
            Set AnchorLblCtrl = Ctrl
        Case Is = "Checkbox"
            Set AnchorChkCtrl = Ctrl
        Case Is = "ComboBox"
            Set AnchorCmbCtrl = Ctrl
        Case Is = "ListBox"
            Set AnchorLstCtrl = Ctrl
        Case Is = "CommandButton"
            Set AnchorCmdCtrl = Ctrl
        Case Is = "Image"
            Set AnchorImgCtrl = Ctrl
        Case Is = "OptionButton"
            Set AnchorOptCtrl = Ctrl
        Case Is = "ToggleButton"
            Set AnchorTglCtrl = Ctrl
    End Select

End Sub

Private Sub form_MouseMove _
(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    lbl.Visible = False: blnNotFirstEntry = False
End Sub

Private Sub GenericMouseMove(Ctrl As Object, ByVal X As Single, ByVal Y As Single)

    If Not blnNotFirstEntry Then
        blnNotFirstEntry = True
        lbl.Visible = True
        Select Case X
            Case Is <= Ctrl.Width / 2
            lbl.Left = Ctrl.Left + X + 10  '(X / 2)
            Case Else
            lbl.Left = Ctrl.Left + (X - lbl.Width)    '(X / 2)
        End Select
        Select Case Y
            Case Is <= Ctrl.Height / 2
            lbl.Top = Ctrl.Top + Y + 10 ' (Y / 2)
            Case Else
            lbl.Top = Ctrl.Top + (Y - lbl.Height)  ' (Y / 2)
        End Select
    End If

End Sub

Private Sub HideToolTip()
    lbl.Visible = False
End Sub


Add a UserForm to your Project , add a few Controls to the userform and run the following Macro in a Standard Module to test the ToolTip Class and see how you can call it in code and how to set its properties :


Code:
Option Explicit

Dim col As New Collection


Sub Test()

    Dim frm As New UserForm1
    Dim objMyToolTip As CustomToolTipClss
    Dim i As Byte
    
    For i = 0 To frm.Controls.Count
        If frm.Controls(i).Tag <> "ToolTip" Then
            Set objMyToolTip = New CustomToolTipClss
            With objMyToolTip
                .AttachTo frm.Controls(i)
                .object.Caption = "Hello from : " & frm.Controls(i).Name
                .object.Font.Size = 10
                .object.Font.Bold = True
                '.object.BackStyle = fmBackStyleTransparent
                .object.ForeColor = vbRed
                .object.BorderStyle = fmBorderStyleSingle
                .object.BackColor = vbYellow
                .object.TextAlign = fmTextAlignLeft
                .object.AutoSize = True
                .object.WordWrap = False
                col.Add objMyToolTip
                Set objMyToolTip.form = frm
            End With
        End If
    Next
    frm.Show

End Sub

Tested in XL 2002 under WIN XP.

Any feedback appreciated. :)

Regards.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Jaafar

Nice code.:)

By the way, I think why it doesn't work for CheckBoxes.
 
Upvote 0
Thanks Norie.

and the reason for not working with CheckBoxws is ..... :biggrin:

Regards.
 
Upvote 0
Jaafar

The capital B.:)

I changed the code to this and it worked for the checkbox.
Code:
Case Is = "CheckBox"
            Set AnchorChkCtrl = Ctrl
I've made the same type of typo in the past.:eek:
 
Upvote 0
Jaafar

The capital B.:)

I changed the code to this and it worked for the checkbox.
Code:
Case Is = "CheckBox"
            Set AnchorChkCtrl = Ctrl
I've made the same type of typo in the past.:eek:


Nice catch Norie .

This is a bit wierd. I tried small letters for the other controls but made no difference. It's only the CheckBox Control that is problematic.

Regards.
 
Upvote 0
Jaafar

Perhaps it might be worth wrapping TypeName(Ctrl) in UCase/LCase?
 
Upvote 0
In response to a previous question,(see here : http://www.mrexcel.com/board2/viewtopic.php?t=214194&highlight=) I managed to put together this reusable custom tooltip Class which uses nothing but a simple label control. However, with a few tricks and in keeping with object oriented prgramming via wrapping the label functionality in a class module the customised label can now be easily added to one's programs as a propper object with its ownd Poperties & Methods thanks to the VBE Intelsense.
...
Add a UserForm to your Project , add a few Controls to the userform and run the following Macro in a Standard Module to test the ToolTip Class and see how you can call it in code and how to set its properties :

Code:
Option Explicit...
...
Any feedback appreciated. :) 
[/QUOTE]
I know this is an old thread, but I arrived here while searching for VBA code to create custom multiline tooltips.

I think the "general" code for the Class Module above is useful, but I have a problem to use it practically in my project, especially the code from the Standard Module (Sub "Test") doesn't suite my needs.

That code creates the [B]same[/B] tooltip for [B]all[/B] the controls on one user form, while I need code to create [B]different[/B] tooltip(s) for a couple of controls on a user form. E.g., I want a tooltip for one specific checkbox and another tooltip for another specific checkbox and still another tooltip for one of the textboxes.

I have made the Class Module as presented above by Jaafar, and tried to add some code from the Standard Module to the UserForm_Initialize event of the form with e.g. ".AttachTo Me.MyCheckBox" and other Properties of the "objMyToolTip", but it doesn't work.

I wonder how the code from Jaafar's Standard Module has to be adapted to suite my needs: add a different custom tooltip for e.g. a checkbox and a textbox of my form, without opening the form from a Macro in a Standard Module.

Thanks.
 
Upvote 0

Forum statistics

Threads
1,214,994
Messages
6,122,633
Members
449,092
Latest member
bsb1122

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