Jaafar Tribak
Well-known Member
- Joined
- Dec 5, 2002
- Messages
- 9,621
- Office Version
- 2016
- Platform
- 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
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 :
Tested in XL 2002 under WIN XP.
Any feedback appreciated.
Regards.
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.