-
February 5th, 2007, 06:17 AM
#1
Tooltips for each ListBox Item in visual basic 6
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
-
February 5th, 2007, 07:44 AM
#2
Re: Tooltips for each ListBox Item in visual basic 6
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 ?
-
February 5th, 2007, 07:55 AM
#3
Re: Tooltips for each ListBox Item in visual basic 6
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.
Last edited by HanneSThEGreaT; February 5th, 2007 at 02:33 PM.
Reason: Typo
-
February 5th, 2007, 09:40 AM
#4
Re: Tooltips for each ListBox Item in visual basic 6
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 !
-
February 5th, 2007, 09:48 AM
#5
Re: Tooltips for each ListBox Item in visual basic 6
Originally Posted by moa
I have a different way to do it with a listbox (without Api)
Please do post
-
February 5th, 2007, 11:03 AM
#6
Re: Tooltips for each ListBox Item in visual basic 6
Originally Posted by HanneSThEGreaT
Please do post
OK, I love the challenges
Back in about minutes, then (the time I need to be sure I am not wrong)
-
February 5th, 2007, 11:13 AM
#7
Re: Tooltips for each ListBox Item in visual basic 6
Well...
It finaly took less time that what I thought :
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
What about the idea ?
Last edited by moa; February 5th, 2007 at 11:16 AM.
-
February 5th, 2007, 11:28 AM
#8
Re: Tooltips for each ListBox Item in visual basic 6
Originally Posted by moa
What about the idea ?
Nice approach!
-
February 5th, 2007, 01:00 PM
#9
Re: Tooltips for each ListBox Item in visual basic 6
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:
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
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)
-
February 5th, 2007, 01:44 PM
#10
Re: Tooltips for each ListBox Item in visual basic 6
Nicely done!
-
February 5th, 2007, 02:30 PM
#11
Re: Tooltips for each ListBox Item in visual basic 6
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 !....
-
January 31st, 2017, 05:33 AM
#12
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.
-
February 1st, 2017, 08:22 PM
#13
Re: Tooltips for each ListBox Item in visual basic 6
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.
Always use [code][/code] tags when posting code.
-
February 1st, 2017, 08:25 PM
#14
Re: Tooltips for each ListBox Item in visual basic 6
What you are forgetting is the original poster is NOT the only one interested in the information. People show up from Google Searches looking for this information?? Yes?? No?? So until this website removes the page altogether the post is never dead
Originally Posted by DataMiser
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.
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
|