Solved

click cell in listview

Posted on 2007-04-01
8
822 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 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
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…

728 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