3

I did not find a native procedure to retrieve the index of the column, the info that is here:

ListView1.ListItems.Item (ListView1.SelectedItem.Index) .ListSubItems (HERE)

I know how to get the index of the line like this:

ListView1.SelectedItem.Index

I know how to recover the position of the mouse like this:

Private Sub ListView1_MouseUp (ByVal Button As Integer, ByVal Shift As Integer, _
                               ByVal x As stdole.OLE_XPOS_PIXELS, _
                               ByVal y As stdole.OLE_YPOS_PIXELS)

I think it should be possible to find this index. Thank you in advance for your help.

braX
  • 11,506
  • 5
  • 20
  • 33
  • You have the `ListView.ColumnHeaders` collection, and each `ColumnHeader` has the `Left` property. – GSerg Jan 31 '18 at 17:06
  • i try this but is not good : `Dim u As Integer, colonne As Integer For u = 1 To ListView1.ColumnHeaders.Count If ListView1.ColumnHeaders(u).Left = x Then colonne = u Exit For End If If ListView1.ColumnHeaders(u).Left > x Then colonne = u - 1 Exit For End If Next u` – Thomas Rogeaux Jan 31 '18 at 17:25
  • 2
    It sounds like you're trying to use a listview as a grid. When you select an item in a listview, you should only need to know that the entire row is selected, not a specific column. Why not use flexgrid instead? It's not that complicated. – Bill Hileman Jan 31 '18 at 18:00
  • if i add 'CLng (x) - (16 * (CLng (u) - 1))' it's more just. but not realy preci. i think that it's the icon – Thomas Rogeaux Jan 31 '18 at 18:41
  • 2
    This has all looks of an X-Y problem. If you need a grid, use a grid. – Mathieu Guindon Jan 31 '18 at 18:53

1 Answers1

1

The native way is using LVM_SUBITEMHITTEST.

Module:

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 Const LVM_SUBITEMHITTEST As Long = &H1039
Private Const LVHT_ONITEM        As Long = &HE

Private Declare Function SendMessage Lib "user32" Alias "SendMessageW" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Form:

Private Sub yourListView_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Dim hitTest As LVHITTESTINFO

    With hitTest
        .flags = LVHT_ONITEM
        .pt.X = (X \ Screen.TwipsPerPixelX)
        .pt.Y = (Y \ Screen.TwipsPerPixelY)
    End With

    SendMessage yourListView.hwnd, LVM_SUBITEMHITTEST, 0, hitTest

    If (hitTest.iItem < 0) Then Exit Sub

    If hitTest.iSubItem = 0 Then
        MsgBox yourListView.ListItems(hitTest.iItem + 1).Text
    Else
        MsgBox yourListView.ListItems(hitTest.iItem + 1).SubItems(hitTest.iSubItem)
    End If
End Sub
Alex K.
  • 171,639
  • 30
  • 264
  • 288