|
-
Apr 12th, 2010, 08:32 AM
#1
Thread Starter
Fanatic Member
Hover over listbox and show value
This little sniplet of code allows you to hover over a listbox and select the item the mouse is over and also show the items value. It may also come in handy if you want to show a tooltip text of the item your over. Hope it maybe usfull.
You need to place a listbox and a label control on your form, Then paste the code below in to the general selection of your form.
Code:
Private Declare Function GetScrollPos Lib "user32.dll" (ByVal hwnd As Long, ByVal nBar As Long) As Long
Private Sub Form_Load()
Dim X As Integer
'Add some items to a listbox
For X = 0 To 50
Call List1.AddItem("This item index is: " & CStr(X))
Next X
List1.FontSize = 18
Set Me.Font = List1.Font
End Sub
Private Sub List1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim yPos As Long
'Get Item Position in the list
yPos = (Y \ Me.TextHeight("A")) + GetScrollPos(List1.hwnd, &H1)
'Move to the list index
List1.ListIndex = yPos
'Show listbox index value in label
Label1.Caption = List1.List(yPos)
End Sub
-
Apr 13th, 2010, 07:37 AM
#2
Re: Hover over listbox and show value
Ben, thanks for sharing. One item: this only works if the Listbox has
the same font as the form. Here's some code for HitTesting 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 LBItemFromPt Lib "comctl32.dll" (ByVal hLB As Long, ByVal ptx As Long, ByVal pty As Long, ByVal bAutoScroll As Long) As Long
Private Function HitTest(ByVal LWnd As Long) As Long
'returns -1 for blank area
Dim pt As POINTAPI
GetCursorPos pt
HitTest = LBItemFromPt(List1.hwnd, pt.X, pt.Y, False)
End Function
Private Sub Form_Load()
Dim i As Long
For i = 1 To 10
List1.AddItem "Item " & i
Next
End Sub
Private Sub List1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim yPos As Long
yPos = HitTest(List1.hwnd)
If yPos > -1 Then
List1.ListIndex = yPos
Label1.Caption = List1.List(yPos)
End If
End Sub
-
Apr 13th, 2010, 09:07 AM
#3
Re: Hover over listbox and show value
Ben, VBClassicRocks' comments are valid.
I thought I add this: One can set the form's Font to the listbox's Font, permanently/temporarily before calling your routine.
Also, you shouldn't need to call GetScrollPos. You could use the listbox's .TopIndex property instead, and you'd have an API-less solution.
-
Apr 13th, 2010, 10:28 AM
#4
Thread Starter
Fanatic Member
Re: Hover over listbox and show value
Thanks for the code VBClassicRocks, Thanks LaVolpe i did not know about the TopIndex property
-
Feb 26th, 2014, 09:13 AM
#5
New Member
Re: Hover over listbox and show value
Hi Folks,
Thanks so much, just found this old thread with Google. I wasn't able to make LBItemFromPt work, not sure why, but the "API-less" approach works fine. I was working with Y as a Single (from the mouse move info), so I got tripped up at first by VB's default rounding, but then I used an Int cast and was good. I wasn't sure how much swapping fonts impacted speed and resources, so I just did it once and stored the row height value as a member variable for my form. (All these values were in Twips, btw.) I left it to the calling routine to check for out-of-range values (such as MyListBoxHitTest(Y!) >= MyListBox.ListCount).
Code:
Function MyListBoxHitTest(ByVal Y!) As Integer
If (mMyListRowHeight% = 0) Then
Dim OldFont As StdFont
Set OldFont = Me.Font
Set Me.Font = MyListBox.Font
mMyListRowHeight% = Me.TextHeight("X")
Set Me.Font = OldFont
End If
MyListBoxHitTest = Int(Y / mMyListRowHeight%) + MyListBox.TopIndex
End Function
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
|