|
-
January 31st, 2017, 05:33 AM
#6
Re: Tooltips for each ListBox Item in visual basic 6
Sorry to say but this method is BUGGY. It sometimes translates to the wrong list index. Try this one:
Top of Module ...
Code:
Private Type POINTAPI
X As Long
Y As Long
End Type
Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
Private Declare Function LBItemFromPt Lib "COMCTL32.DLL" (ByVal hLB As Long, ByVal ptX As Long, ByVal ptY As Long, ByVal bAutoScroll As Long) As Long
Code:
Private Sub List1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
On Error Resume Next
List1.ToolTipText = L.List(ItemUnderMouse(List1.hwnd, X, Y))
End Sub
Code:
Private Function ItemUnderMouse(ByVal list_hWnd As Long, ByVal X As Single, ByVal Y As Single)
Dim pt As POINTAPI: pt.X = X \ Screen.TwipsPerPixelX: pt.Y = Y \ Screen.TwipsPerPixelY
ClientToScreen list_hWnd, pt
ItemUnderMouse = LBItemFromPt(list_hWnd, pt.X, pt.Y, False)
End Function
 Originally Posted by HanneSThEGreaT
Yeah, there's a difference between ComboBox and ListBox
This is how to do it with a ListBox :
Code:
Option Explicit
Private Type POINTAPI
X As Long
Y As Long
End Type
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function ScreenToClient Lib "user32" (ByVal hWnd As Long, lpPoint As POINTAPI) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long
Private Const LB_ITEMFROMPOINT = &H1A9
Private Sub Form_Load()
Dim iIndex As Integer
For iIndex = 1 To 100
List1.AddItem "List Item " & iIndex
Next
End Sub
Private Sub List1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim tPOINT As POINTAPI
Dim iIndex As Long
'get the Mouse Cursor Position
Call GetCursorPos(tPOINT)
'Convert the Coords to be Relative to the Listbox
Call ScreenToClient(List1.hWnd, tPOINT)
'Find which Item the Mouse is Over
iIndex = SendMessage(List1.hWnd, LB_ITEMFROMPOINT, 0&, ByVal ((tPOINT.X And &HFF) Or (&H10000 * (tPOINT.Y And &HFF))))
If iIndex >= 0 Then
'Extract the List Index
iIndex = iIndex And &HFF
'set the Lists ToolTipText
List1.ToolTipText = List1.List(iIndex)
End If
End Sub
Now, if I were to do the same (well almost the same  ) with the combobox :
Code:
Option Explicit
Private Type POINTAPI
X As Long
Y As Long
End Type
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function ScreenToClient Lib "user32" (ByVal hWnd As Long, lpPoint As POINTAPI) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long
Private Const LB_ITEMFROMPOINT = &H1A9
Const CB_SHOWDROPDOWN = &H14F
'to open cb list automatically
Sub ComboBoxOpenList(cbo As ComboBox, Optional showIt As Boolean = True)
SendMessage cbo.hWnd, CB_SHOWDROPDOWN, showIt, ByVal 0&
End Sub
Private Sub Combo1_DropDown() 'drop down event
Dim tPOINT As POINTAPI
Dim iIndex As Long
'get the Mouse Cursor Position
Call GetCursorPos(tPOINT)
'Convert the Coords to be Relative to the Listbox
Call ScreenToClient(Combo1.hWnd, tPOINT)
'Find which Item the Mouse is Over
iIndex = SendMessage(Combo1.hWnd, LB_ITEMFROMPOINT, 0&, ByVal ((tPOINT.X And &HFF) Or (&H10000 * (tPOINT.Y And &HFF))))
If iIndex >= 0 Then
'Extract the List Index
iIndex = iIndex And &HFF
'set the Lists ToolTipText
Combo1.ToolTipText = Combo1.List(iIndex)
End If
End Sub
Private Sub Form_Load()
Dim iIndex As Integer
'Fill the List with Dummy Values
For iIndex = 1 To 100
Combo1.AddItem "Item " & iIndex
Next
ComboBoxOpenList Combo1, True 'automatically open dropdown list of combo
End Sub
The problem here is that it will ONLY show the tooltips when the list isn't dropped anymore.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|