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

You might also like...

Comments

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.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“The generation of random numbers is too important to be left to chance.” - Robert R. Coveyou