?
Solved

click cell in listview

Posted on 2007-04-01
8
Medium Priority
?
823 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

777 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