Results 1 to 7 of 7

Thread: [RESOLVED] Selected subitem in a listview

  1. #1

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Resolved [RESOLVED] Selected subitem in a listview

    How do I determine which listview column has been clicked on?
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Selected subitem in a listview

    Would this work for you?
    VB Code:
    1. Option Explicit
    2.  
    3. Private Const LVM_FIRST = &H1000
    4. Private Const LVHT_NOWHERE As Long = &H1
    5. Private Const LVHT_ONITEMICON As Long = &H2
    6. Private Const LVHT_ONITEMLABEL As Long = &H4
    7. Private Const LVHT_ONITEMSTATEICON As Long = &H8
    8. Private Const LVHT_ONITEM As Long = (LVHT_ONITEMICON Or LVHT_ONITEMLABEL Or LVHT_ONITEMSTATEICON)
    9. Private Const LVM_SUBITEMHITTEST As Long = (LVM_FIRST + 57)
    10.  
    11. Private Type POINTAPI
    12.    x As Long
    13.    y As Long
    14. End Type
    15.  
    16. Private Type HITTESTINFO
    17.    pt As POINTAPI
    18.    flags As Long
    19.    iItem As Long
    20.    iSubItem  As Long
    21. End Type
    22.  
    23. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
    24.   (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    25.  
    26. Dim HTI As HITTESTINFO
    27.  
    28. Private Sub Form_Load()
    29. '=======================
    30. Dim i%, j%
    31. Dim itm As ListItem
    32. Dim LSI As ListSubItem
    33.  
    34.     ListView1.ColumnHeaders.Add , , "Item"
    35.     ListView1.ColumnHeaders.Add , , "SubItem1"
    36.     ListView1.ColumnHeaders.Add , , "SubItem2"
    37.     ListView1.ColumnHeaders.Add , , "SubItem3"
    38.    
    39.     For i = 1 To 5
    40.         Set itm = ListView1.ListItems.Add(, , "Item " & i)
    41.         For j = 1 To 3
    42.             Set LSI = itm.ListSubItems.Add(, , "Subitem " & i & j)
    43.         Next j
    44.     Next
    45.  
    46. End Sub
    47.  
    48. Private Sub ListView1_DblClick()
    49. '================================
    50. Dim iRes As Long
    51. Dim iItemIndex As Long
    52. Dim iSubitemIndex As Long
    53. Dim sMsg As String
    54.  
    55. On Error Resume Next
    56.  
    57.     iRes = SendMessage(ListView1.hwnd, LVM_SUBITEMHITTEST, 0, HTI)
    58.    
    59.     iItemIndex = HTI.iItem + 1
    60.     iSubitemIndex = HTI.iSubItem
    61.    
    62.     ListView1.ListItems(iItemIndex).Selected = True
    63.    
    64.     sMsg = "Selected Item:    " & ListView1.ListItems(iItemIndex).Text & vbNewLine & _
    65.            "Selected SubItem: " & ListView1.ListItems(iItemIndex).SubItems(iSubitemIndex)
    66.    
    67.     MsgBox sMsg
    68.    
    69.     sMsg = "Row Index: " & iItemIndex & vbNewLine & _
    70.            "Col Index: " & iSubitemIndex + 1 & vbNewLine & _
    71.            "Col Name : " & ListView1.ColumnHeaders(iSubitemIndex + 1)
    72.    
    73.     MsgBox sMsg
    74.  
    75. End Sub
    76.  
    77. Private Sub ListView1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
    78.      With HTI
    79.         .pt.x = (x / Screen.TwipsPerPixelX)
    80.         .pt.y = (y / Screen.TwipsPerPixelY)
    81.         .flags = LVHT_ONITEMICON Or LVHT_ONITEMLABEL Or LVHT_NOWHERE
    82.     End With
    83. End Sub

  3. #3
    Frenzied Member zynder's Avatar
    Join Date
    Nov 2006
    Location
    localhost
    Posts
    1,434

    Re: Selected subitem in a listview

    Cool. Gonna save this code for future use.

  4. #4

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: [RESOLVED] Selected subitem in a listview

    Cool, indeed! Thanks a lot.

    However, is it really necessary to go through all that code? What about checking (in the listview mousedown event) the x coordinate against the column boundaries, would that work?
    Last edited by krtxmrtz; Feb 8th, 2007 at 03:55 AM.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  5. #5

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: [RESOLVED] Selected subitem in a listview

    Further, is the 9th line of the double click event handler necessary?

    VB Code:
    1. ListView1.ListItems(iItemIndex).Selected = True

    I've found the code works equally well without it.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  6. #6

  7. #7
    Addicted Member
    Join Date
    Aug 2006
    Posts
    211

    Re: [RESOLVED] Selected subitem in a listview

    Very nice! This is just what I was looking for!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width