Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 37,910 times

Related Categories

Listview SubItem Click Detection

Kimmy

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

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.

Comments

  • Clicked value=

    Posted by dimsis on 24 Jun 2004

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

  • Posted by didge on 21 Jun 2004

    found this... it does the trick..

    http://www.syncfusion.com/faq/winforms/Files/WForum_ListViewSubItem.zip

  • .net ???

    Posted by didge on 21 Jun 2004

    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...

  • .net

    Posted by didge on 21 Jun 2004

    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 t...

  • Listview hit test error

    Posted by allison_zarra on 17 Feb 2004

    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 SendMes...