'display the userform that contains the listbox.
Lbx.Parent.Show
**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
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.
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!
Yeah, it represents correct latin characters only. Would be good to make an option to switch codings or involve multi-language feature. Thanks!Are you talking about the text that is displayed on the custom tooltip ?
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
'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
@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
'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