Results 1 to 5 of 5

Thread: Hover over listbox and show value

  1. #1

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    814

    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

  2. #2
    Fanatic Member
    Join Date
    Mar 2009
    Posts
    804

    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

  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  4. #4

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    814

    Re: Hover over listbox and show value

    Thanks for the code VBClassicRocks, Thanks LaVolpe i did not know about the TopIndex property

  5. #5
    New Member
    Join Date
    Feb 2014
    Posts
    1

    Thumbs up 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
  •  



Click Here to Expand Forum to Full Width