Library code snippets

Listview SubItem Click Detection

With this bit of code you will be able to know if a click on a listview control was made over any subitem. The example below shows the logic to that in the DblClick event of the listview control.

You Need:  Listview1 ~ create some columns and set View to lvwReport.

Double click any column/row on the Listview1's grid to see the results. Note that you won't see anything unless you fill up the listview with a few items; this can be done for testing purposes in the Form_Load() event.

' In General Declarations
Private Const LVM_FIRST As Long = &H1000
Private Const LVM_HITTEST As Long = (LVM_FIRST + 18)
Private Const LVM_SUBITEMHITTEST As Long = (LVM_FIRST + 57)
Private Const LVHT_ONITEMICON As Long = &H2
Private Const LVHT_ONITEMLABEL As Long = &H4
Private Const LVHT_ONITEMSTATEICON As Long = &H8
Private Const LVHT_ONITEM As Long = (LVHT_ONITEMICON Or _
                                    LVHT_ONITEMLABEL Or _
                                    LVHT_ONITEMSTATEICON)
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

Dim lX as single, lY as single

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

Private Sub ListView1_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
   lX = x
   lY = y
End Sub

Private Sub ListView1_DblClick()
Dim HTI As LVHITTESTINFO

  With HTI
     .pt.x = (lX \ Screen.TwipsPerPixelX)
     .pt.y = (lY \ Screen.TwipsPerPixelY)
     .flags = LVHT_ONITEM
  End With
     
  Call SendMessage(ListView1.hwnd, LVM_SUBITEMHITTEST, 0, HTI)

Dim lst As ListItem
   If (HTI.iItem > -1) Then
       Set lst = ListView1.ListItems(HTI.iItem + 1)

       msgbox  "Clicked item " & HTI.iItem + 1 & " and SubItem " & HTI.iSubItem
   End If
End Sub

Comments

  1. 24 Jun 2004 at 01:07

    Clicked value=
    ListView1.SelectedItem.SubItems(HTI.iSubItem)

  2. 21 Jun 2004 at 07:21
  3. 21 Jun 2004 at 06:44

    has anybody managed to get this working with vb.net?


    ive been forced to use the original comctrl version of the list view since the vb.net version doesnt support subitem icons!
    ... so ive hacked the code into my .net app, it runs, but always returns 0,0.


    Regards


    DiDGE

  4. 21 Jun 2004 at 01:22

    has anybody managed to get this working with vb.net?


    ive been forced to use the original comctrl version of the list view since the vb.net version doesnt support subitem icons!
    ... so ive hacked the code into my .net app, it runs, but always returns 0,0.


    Regards


    DiDGE

  5. 17 Feb 2004 at 14:33

    When I use this code in vb.net, it complains about the keyword "Any" in the declarations, so I substituted "object" but I also get an error when this line of code tries to execute:        Call SendMessage(ListView1.hWnd, LVM_SUBITEMHITTEST, 0, HTI)  


    The error is System.ExecutionEngineException.


    I had also tried creating this project in vb6 and had vb.net convert it, and I get the following warning:


    'UPGRADEWARNING: Couldn't resolve default property of object HTI. Click for more: 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="vbup1037"'
           Call SendMessage(ListView1.hWnd, LVM
    SUBITEMHITTEST, 0, HTI)


    Any help would be greatly appreciated.

  6. 25 Nov 2003 at 07:30

     Thanks a lot for the code.. saved lot of time ...


    Prasanna

  7. 10 Nov 2003 at 09:04

    How i can get the value of the clicked cell?

  8. 23 Sep 2002 at 01:58
    is it possible to convert the listview control right to left ?

    If u have any sample code pls send to my account Reddy_cse@rediffmail.com
  9. 28 Aug 2002 at 17:08

    a very usefull code.........

  10. 01 Jan 1999 at 00:00

    This thread is for discussions of Listview SubItem Click Detection.

Leave a comment

Sign in or Join us (it's free).

Kym Manson Till the Roof comes off Till the Lights go out Till my Legs give out Can't shut my mouth I will not fall, my Wisdoms all.

Related discussion

Related podcasts

  • Christian Beauclair

    14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...

Want to stay in touch with what's going on? Follow us on twitter!