Can anybody Suggest How to add ToolTip In the Combox while It is droped down. So that when we move mouse on the drop down box we can see the combo box s item in the tooltip
Printable View
Can anybody Suggest How to add ToolTip In the Combox while It is droped down. So that when we move mouse on the drop down box we can see the combo box s item in the tooltip
Your title says "listbox" while you question is concerning a "combobox" !!!
Now : it is possible to do it with a listbox and it is not with a combobox (for the simple reason that a listbox responds to the mouse-move event while a combobox does not ...)
So .... Is it a listbox or a combobox ?
Yeah, there's a difference between ComboBox and ListBox :)
This is how to do it with a ListBox :
Now, if I were to do the same (well almost the same :D ) 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
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
The problem here is that it will ONLY show the tooltips when the list isn't dropped anymore.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
I have a different way to do it with a listbox (without Api)
It would be possible to do it with a ComboBox and Apis, but at a very high cost in terms of code and execution time !
Please do post :)Quote:
Originally Posted by moa
OK, I love the challengesQuote:
Originally Posted by HanneSThEGreaT
Back in about minutes, then (the time I need to be sure I am not wrong) :)
Well...
It finaly took less time that what I thought :
What about the idea ?Code:Private h As Single
Private Sub Form_Activate()
'All this in blue) is here only to make it easier for you to check
'you can, for instance, change the fontsize
List1.FontSize = 10
For I = 0 To 40
List1.AddItem I & "a"
Next
With Font
Set Font = List1.Font
End With
h = TextHeight("aaa") * 1.02
End Sub
Private Sub List1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
where_am_i = List1.TopIndex + Int(Y / h)
List1.ToolTipText = List1.List(where_am_i)
End Sub
Nice approach! :thumb: :)Quote:
Originally Posted by moa
if you're attempting to use the ToolTipText to show the full text of items wider than the dropdown, then I would recommend using CB_SETDROPPEDWIDTH to change the width of the dropdown instead:obviously, depending on your circumstances, the code to determine the width should be just calculated once, rather than everytime it needs to drop down. The 30 in that Sub is also just a completely arbitrary number to allow for scrollbar / border (you could calculate it properly using GetSystemMetrics if you wished)Code:Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const CB_SETDROPPEDWIDTH As Long = &H160&
Private Sub Combo1_DropDown()
Dim N As Long, lLen As Long, lLongest As Long
With Combo1
Set Me.Font = .Font
For N = 0 To .ListCount - 1
lLen = Me.TextWidth(.List(N))
If lLen > lLongest Then lLongest = lLen
Next N
SendMessage .hwnd, CB_SETDROPPEDWIDTH, Me.ScaleX(lLongest, Me.ScaleMode, vbPixels) + 30, 0&
End With
End Sub
Private Sub Form_Load()
Dim N As Long
For N = 1 To 20
Combo1.AddItem String$(40 * Rnd, "A")
Next N
End Sub
Nicely done! :thumb:
It is definitely very very nice !
May I make a confession, now ?
The old man I am (I am really old) was on the point to :
- remind that a combobox is, finaly, nothing else but the combination of a textbox and a listbox
- then propose to combinate a textbox and a listbox and treats the listbox
I suppose one becomes lazzy when getting aged !.... :D
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
Another old thread dug up.
Question was asked 10 years ago. I think it safe to say that the questioner is no longer looking for the answer.
You really should look at the date on the thread and let those which have been dead for years stay dead.