Link to home
Start Free TrialLog in
Avatar of AHMKC1
AHMKC1

asked on

click cell in listview

How I find the mouse click position (or Cell value)  in a listview in vb 6
Avatar of Mark_FreeSoftware
Mark_FreeSoftware
Flag of Netherlands image

try this code:

Option Explicit

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

Const LVM_FIRST = &H1000&
Const LVM_HITTEST = LVM_FIRST + 18

Private Type POINTAPI
    X As Long
    Y As Long
End Type

Private Type LVHITTESTINFO
    pt As POINTAPI
    flags As Long
    iItem As Long
    iSubItem As Long
End Type


Private Sub Form_Load()
    With ListView1.ListItems
        .Add Text:="Test item #1"
        .Add Text:="Test item #2"
        .Add Text:="Long long long test item #3"
    End With
End Sub

Private Sub ListView1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim lvhti As LVHITTESTINFO
Dim lItemIndex As Long
Static m_lCurItemIndex As Long
    lvhti.pt.X = X / Screen.TwipsPerPixelX
    lvhti.pt.Y = Y / Screen.TwipsPerPixelY
    lItemIndex = SendMessage(ListView1.hwnd, LVM_HITTEST, 0, lvhti) + 1
    If m_lCurItemIndex <> lItemIndex Then
        m_lCurItemIndex = lItemIndex
        If m_lCurItemIndex <> 0 Then
            Me.Caption = ListView1.ListItems(m_lCurItemIndex).Text
        End If
    End If
End Sub
ASKER CERTIFIED SOLUTION
Avatar of Mark_FreeSoftware
Mark_FreeSoftware
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This code will give u the exact subitem clicked
http://vbnet.mvps.org/index.html?code/comctl/lvhittest.htm

hey DanRollins,

think my second answer (the second answer posted also) is the best one,
it does exactly what the Author asks



//

thank you for your continuing work of cleaning those rather old questions!