How to determine the VB6 List values base on the X, Y mouse move position?

Posted on 2007-07-23
Last Modified: 2010-04-30
How can I get the list value from a VB6 List box from the X, and Y positions in the mouse over event?  No item in the list box is choosen.  However, as the user does a mouse over the control want to display additional information for the value in the listbox the that mouse is over.  

It is very easy to get the X and Y positions from the mouse move event.  Is there an API call or some method to determine the list value based on the X and Y position of the mouse move event?  Actually, I will not just be displaying the list value.  But, based on the list value I will display additional informtion.

Question by:UBTUCE
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    I'll post a bare bones example later tonight if you can't make heads or tails of what I have done in the links above just be patient.  ;)
    LVL 85

    Accepted Solution

    Create a New Project and add a Label and a ListBox:

    Option Explicit

    Private Const LB_GETITEMHEIGHT = &H1A1

    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 Sub Form_Load()
        Dim i As Integer
        For i = 1 To 100
            List1.AddItem "Item" & i
    End Sub

    Private Sub List1_MouseMove(Button As Integer, Shift As Integer, x As Single, Y As Single)
        Dim index As Integer
        index = listrowcalc(List1, Y)
        Label1.Caption = List1.List(index)
    End Sub

    Private Function listrowcalc(lstTemp As Control, ByVal Y As Single) As Integer
        Dim ItemHeight As Integer
        ItemHeight = SendMessage(lstTemp.hWnd, LB_GETITEMHEIGHT, 0, 0)
        listrowcalc = min(((Y / Screen.TwipsPerPixelY) \ ItemHeight) + _
            lstTemp.TopIndex, lstTemp.ListCount - 1)
    End Function

    Private Function min(x As Integer, Y As Integer) As Integer
        If x > Y Then min = Y Else min = x
    End Function

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Suggested Solutions

    There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
    Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now