Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

click cell in listview

Posted on 2007-04-01
8
Medium Priority
?
826 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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 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…
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…
Suggested Courses

618 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