Tooltips for a listbox

dcanham

Active Member
Joined
Jun 7, 2006
Messages
306
Is there a way to have a tooltip for each individual item in a listbox. I know how to have a tooltip for the overall list box object, but not for the individual line items.
 
Ok, works like a charm. I did have to ".Hide" the form before I ran the AttachToolTipToListBox routine, otherwise the line:
Code:
    'display the userform that contains the listbox.
    Lbx.Parent.Show
would throw a modal error. Now I have to try and adapt it for loading the custom descriptions from an mdb backend I'm using. If you like I can send you the project when it's done? Thanks again for your help :)
 
Upvote 0

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Jaafar

Good work as usual.:)

I've not studied the whole code, or tried it out yet - just wondering if/how it deals/will deal with a multiselect listbox.
 
Upvote 0
Ok, works like a charm. I did have to ".Hide" the form before I ran the AttachToolTipToListBox routine, otherwise the line:

Code:

'display the userform that contains the listbox. Lbx.Parent.Show</PRE>
would throw a modal error. Now I have to try and adapt it for loading the custom descriptions from an mdb backend I'm using. If you like I can send you the project when it's done? Thanks again for your help :)
**
dcanham.

Yes it would because the AttachToolTipToListBox routine launches the usefrorm already. The example i posted was just for illustration purposes, you will obviously need to adapt it to your own project logic. **


Jaafar

Good work as usual.:)

I've not studied the whole code, or tried it out yet - just wondering if/how it deals/will deal with a multiselect listbox.

Thanks Norie.

the multiselect property shouldn't have an effect on the code as this works on the mouse pointer position regardless of what is selected.

What would probably be more difficult is to make this work for listboxes/comboboxes on worksheets.

Regards.
 
Upvote 0
Big thanks Jaafar! The code is amazing! But the question is how can I change the code page? I mean when I type a text for tooltip in Russian it's unreadable. Thanks for answer in advance!
 
Upvote 0
Big thanks Jaafar! The code is amazing! But the question is how can I change the code page? I mean when I type a text for tooltip in Russian it's unreadable. Thanks for answer in advance!

Sorry for getting back late. Are you talking about the text that is displayed on the custom tooltip ?

I'll investigate this cross-language issue further and get back if I know the answer. I too, want to be able to display Arabic characters.
 
Upvote 0
Why make it easy when you can make it hard ?


Code:
Private Sub ListBox1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If Events Then
    Dim list_index As Long, Zero As Long
    Zero = Me.ListBox1.TopIndex
    list_index = (Y) \ (8 + 2) + Zero ' ; 8 si the size of text/Font
    'MsgBox (Y & " , " & list_index)
    'Me.ChMarchand = Y 'test label
    'Me.AmitieMarchand = list_index 'test label
    'Me.Ville = Zero 'test label
    With Me.ListBox1
        .ControlTipText = .List(list_index, 1)
    End With
End If
End Sub
 
Upvote 0
@Jaafar Tribak tried the code couldn't get working on Excel 365 Windows 10 tried to mod for 64bit but still didn't work and a lot of code to try and figure out. I've come across the following code else where as referenced and looks promising with a lot less code but still struggling to get to work any ideas on this one? I've tried my best to port it over from VB Helper: HowTo: Make a ListBox display a different tooltip for each item under the mouse in Visual Basic 6

VBA Code:
'http://www.vb-helper.com/howto_listbox_item_tooltips.html
Private Type POINTAPI
    X As LongPtr
    Y As LongPtr
End Type

Private Const SM_CXSCREEN = 0
Private Const SM_CYSCREEN = 1

'https://www.excelbanter.com/excel-programming/396436-how-get-screen-resolution-vba.html
Private Declare PtrSafe Function GetSystemMetrics Lib "User32" (ByVal nIndex As LongPtr) As LongPtr
Private Declare PtrSafe Function ClientToScreen Lib "User32" (ByVal hwnd As LongPtr, lpPoint As POINTAPI) As LongPtr
Private Declare PtrSafe Function LBItemFromPt Lib "COMCTL32.DLL" (ByVal hLB As LongPtr, ByVal ptX As LongPtr, ByVal ptY As LongPtr, ByVal bAutoScroll As LongPtr) As LongPtr

Private m_TooltipText() As String
Public Function ScreenHeight() As LongPtr
ScreenHeight = GetSystemMetrics(SM_CYSCREEN)
End Function

Public Function ScreenWidth() As LongPtr
ScreenWidth = GetSystemMetrics(SM_CXSCREEN)
End Function
Private Sub UserForm_Initialize()

'Load Example Listbox
With ListBox1

For Each Item In Array("Select an item", "Apple fritters", "Banana pie", "Cherriers jubilee", Date, "Ribbet", "", "Sorry, no help for you!")
.AddItem Item
Next
End With

End Sub
Private Sub Form_Load()
    ReDim m_TooltipText(-1 To List1.ListCount - 1)
    m_TooltipText(-1) = "Select an item"
    m_TooltipText(0) = "Apple fritters"
    m_TooltipText(1) = "Banana pie"
    m_TooltipText(2) = "Cherriers jubilee"
    m_TooltipText(3) = Date
    m_TooltipText(4) = "Ribbet"
    m_TooltipText(5) = ""
    m_TooltipText(6) = "Sorry, no help for you!"
End Sub
' See which item is under the mouse and display its tooltip.
Private Sub ListBox1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    ListBox1.ControlTipText = m_TooltipText(ItemUnderMouse(ListBox1.[_GethWnd], X, Y))
End Sub
' Return the index of the item under the mouse.
Public Function ItemUnderMouse(ByVal list_hWnd As LongPtr, ByVal X As Single, ByVal Y As Single)

Dim pt As POINTAPI

    pt.X = X \ ScreenWidth  'Screen.TwipsPerPixelX - not part of native VBA
    pt.Y = Y \ ScreenHeight  'Screen.TwipsPerPixelY  - not part of native VBA

    ClientToScreen list_hWnd, pt
    ItemUnderMouse = LBItemFromPt(list_hWnd, pt.X, pt.Y, False)
    
End Function
 
Upvote 0
@Jaafar Tribak tried the code couldn't get working on Excel 365 Windows 10 tried to mod for 64bit but still didn't work and a lot of code to try and figure out. I've come across the following code else where as referenced and looks promising with a lot less code but still struggling to get to work any ideas on this one? I've tried my best to port it over from VB Helper: HowTo: Make a ListBox display a different tooltip for each item under the mouse in Visual Basic 6

VBA Code:
'http://www.vb-helper.com/howto_listbox_item_tooltips.html
Private Type POINTAPI
    X As LongPtr
    Y As LongPtr
End Type

Private Const SM_CXSCREEN = 0
Private Const SM_CYSCREEN = 1

'https://www.excelbanter.com/excel-programming/396436-how-get-screen-resolution-vba.html
Private Declare PtrSafe Function GetSystemMetrics Lib "User32" (ByVal nIndex As LongPtr) As LongPtr
Private Declare PtrSafe Function ClientToScreen Lib "User32" (ByVal hwnd As LongPtr, lpPoint As POINTAPI) As LongPtr
Private Declare PtrSafe Function LBItemFromPt Lib "COMCTL32.DLL" (ByVal hLB As LongPtr, ByVal ptX As LongPtr, ByVal ptY As LongPtr, ByVal bAutoScroll As LongPtr) As LongPtr

Private m_TooltipText() As String
Public Function ScreenHeight() As LongPtr
ScreenHeight = GetSystemMetrics(SM_CYSCREEN)
End Function

Public Function ScreenWidth() As LongPtr
ScreenWidth = GetSystemMetrics(SM_CXSCREEN)
End Function
Private Sub UserForm_Initialize()

'Load Example Listbox
With ListBox1

For Each Item In Array("Select an item", "Apple fritters", "Banana pie", "Cherriers jubilee", Date, "Ribbet", "", "Sorry, no help for you!")
.AddItem Item
Next
End With

End Sub
Private Sub Form_Load()
    ReDim m_TooltipText(-1 To List1.ListCount - 1)
    m_TooltipText(-1) = "Select an item"
    m_TooltipText(0) = "Apple fritters"
    m_TooltipText(1) = "Banana pie"
    m_TooltipText(2) = "Cherriers jubilee"
    m_TooltipText(3) = Date
    m_TooltipText(4) = "Ribbet"
    m_TooltipText(5) = ""
    m_TooltipText(6) = "Sorry, no help for you!"
End Sub
' See which item is under the mouse and display its tooltip.
Private Sub ListBox1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    ListBox1.ControlTipText = m_TooltipText(ItemUnderMouse(ListBox1.[_GethWnd], X, Y))
End Sub
' Return the index of the item under the mouse.
Public Function ItemUnderMouse(ByVal list_hWnd As LongPtr, ByVal X As Single, ByVal Y As Single)

Dim pt As POINTAPI

    pt.X = X \ ScreenWidth  'Screen.TwipsPerPixelX - not part of native VBA
    pt.Y = Y \ ScreenHeight  'Screen.TwipsPerPixelY  - not part of native VBA

    ClientToScreen list_hWnd, pt
    ItemUnderMouse = LBItemFromPt(list_hWnd, pt.X, pt.Y, False)
   
End Function

Just changed slightly again:
Code:
'http://www.vb-helper.com/howto_listbox_item_tooltips.html
Private Type POINTAPI
    X As LongPtr
    Y As LongPtr
End Type

Private Const SM_CXSCREEN = 0
Private Const SM_CYSCREEN = 1

'https://www.excelbanter.com/excel-programming/396436-how-get-screen-resolution-vba.html
Private Declare PtrSafe Function GetSystemMetrics Lib "User32" (ByVal nIndex As LongPtr) As LongPtr
Private Declare PtrSafe Function ClientToScreen Lib "User32" (ByVal hwnd As LongPtr, lpPoint As POINTAPI) As LongPtr
Private Declare PtrSafe Function LBItemFromPt Lib "COMCTL32.DLL" (ByVal hLB As LongPtr, ByVal ptX As LongPtr, ByVal ptY As LongPtr, ByVal bAutoScroll As LongPtr) As LongPtr

Private m_TooltipText() As String
Public Function ScreenHeight() As LongPtr
ScreenHeight = GetSystemMetrics(SM_CYSCREEN)
End Function

Public Function ScreenWidth() As LongPtr
ScreenWidth = GetSystemMetrics(SM_CXSCREEN)
End Function
Private Sub UserForm_Initialize()

'Load Example Listbox
With ListBox1

For Each Item In Array("Select an item", "Apple fritters", "Banana pie", "Cherriers jubilee", Date, "Ribbet", "", "Sorry, no help for you!")
.AddItem Item
Next


ReDim m_TooltipText(-1 To List1.ListCount - 1)
m_TooltipText(-1) = "Select an item"
m_TooltipText(0) = "Apple fritters"
m_TooltipText(1) = "Banana pie"
m_TooltipText(2) = "Cherriers jubilee"
m_TooltipText(3) = Date
m_TooltipText(4) = "Ribbet"
m_TooltipText(5) = ""
m_TooltipText(6) = "Sorry, no help for you!"

End With

End Sub
' See which item is under the mouse and display its tooltip.
Private Sub ListBox1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    ListBox1.ControlTipText = m_TooltipText(ItemUnderMouse(ListBox1.[_GethWnd], X, Y))
End Sub
' Return the index of the item under the mouse.
Public Function ItemUnderMouse(ByVal list_hWnd As LongPtr, ByVal X As Single, ByVal Y As Single)

Dim pt As POINTAPI

    pt.X = X \ ScreenWidth  'Screen.TwipsPerPixelX - not part of native VBA
    pt.Y = Y \ ScreenHeight  'Screen.TwipsPerPixelY  - not part of native VBA

    ClientToScreen list_hWnd, pt
    ItemUnderMouse = LBItemFromPt(list_hWnd, pt.X, pt.Y, False)
    
End Function
 
Upvote 0

Forum statistics

Threads
1,216,473
Messages
6,130,837
Members
449,597
Latest member
buikhanhsang

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