Solved

click cell in listview

Posted on 2007-04-01
8
808 Views
Last Modified: 2013-12-25
How I find the mouse click position (or Cell value)  in a listview in vb 6
0
Comment
Question by:AHMKC1
  • 3
  • 2
8 Comments
 
LVL 13

Expert Comment

by:Mark_FreeSoftware
ID: 18833023
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
0
 
LVL 13

Accepted Solution

by:
Mark_FreeSoftware earned 500 total points
ID: 18833035

or even better:



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_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
Dim lvhti As LVHITTESTINFO
Dim lItemIndex As Long
    lvhti.pt.x = x / Screen.TwipsPerPixelX
    lvhti.pt.y = y / Screen.TwipsPerPixelY
    lItemIndex = SendMessage(ListView1.hwnd, LVM_HITTEST, 0, lvhti) + 1
    If lItemIndex <> 0 Then
        MsgBox "You clicked on: " & ListView1.ListItems(lItemIndex).Text, vbOKOnly, ""
    End If
End Sub
0
 
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 18849854
0
 
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 18849903
This code will give u the exact subitem clicked
http://vbnet.mvps.org/index.html?code/comctl/lvhittest.htm
0
 
LVL 13

Expert Comment

by:Mark_FreeSoftware
ID: 19130430

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!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

757 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

23 Experts available now in Live!

Get 1:1 Help Now