We help IT Professionals succeed at work.


tstrob asked
Hi all,

I am trying to get information on a ListViewItem of a ListView control which hWnd is known!
Especially I want to retrieve the Icons index number of the used ImageList.

I tried this way:


Public Type LV_ITEM
    mask        As Long
    iItem       As Long
    iSubItem    As Long
    state       As Long
    stateMask   As Long
    pszText     As Long (Also tried "as String" here)
    cchTextMax  As Long
    iImage      As Long
    lParam      As Long
    iIndent     As Long
End Type

========CODE SECTION====================0

dim myItem as LV_ITEM

myItem.iItem = 1
myItem.iSubItem = 0
myItem.mask = LVIF_IMAGE of LVIF_TEXT

SendMessage hWnd, LVM_GETITEM, 0&, myItem

==========END OF CODE SECTION=====================

But nothing happens with my myItem-Structure... And I have no idea why...
The hWnd is correct. I already tried to send e.g. LVM_DELTEALLITEMS message to the control and all items became deleted... So I think something with the LVM_GETITEM message is wrong... Maybe the LV_ITEM is defined wrong?!

Any ideas from you?

Watch Question

What did you set LVM_GETITEM to in your CONST declaration? (Const LVM_GETITEMA = (LVM_FIRST + 5))

Also - If you think your LV_ITEM is defined wrong, try testing (testing only) with variants to help you with what they type should be.


Public Const LVM_FIRST As Long = &H1000
Public Const LVM_GETITEM As Long = (LVM_FIRST + 5)


This will return the Image index and the Text for the first item in a Listview:

Private Type LV_ITEM
   mask As Long
   iItem As Long
   iSubItem As Long
   state As Long
   stateMask As Long
   pszText As String
   cchTextMax As Long
   iImage As Long
   lParam As Long
   iIndent As Long
End Type

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

Const LVM_FIRST = &H1000
Const LVIF_IMAGE = &H2
Const LVIF_TEXT = &H1

Private Sub Command1_Click()
    Dim myItem As LV_ITEM

    With myItem
        .iItem = 0 '<-- 1'st Listitem (zero based index)
        .iSubItem = 0 '<-- one based; set to 0 to refer to Item
        .pszText = String(256, vbNullChar) '<-- Allocate buffer space for Item text
        .cchTextMax = 256 '<-- Length of buffer
        .mask = LVIF_IMAGE Or LVIF_TEXT
    End With
    SendMessage ListView1.hwnd, LVM_GETITEM, 0&, myItem
    'Remove null char's from Item text string
    myItem.pszText = Replace(myItem.pszText, vbNullChar, "")
    MsgBox "Item Text: " & myItem.pszText & vbCr & _
           "Image Index: " & myItem.iImage

End Sub


I found out another interessting thing.
The workflow described above (mine and also jgv's) would work for ListViews which are created within the same application.

If you create two applications and use the hWnd of the first application to read out the ListvView in the second app, it wont work.

But some commands seem to work, e.g. the LVM_DELETEALLITEMS, which the second app can fire to get the ListView of the first app deleted...

Any hints?

Author of the Year 2009

Hi tstrob,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Refund points and save as a 0-pt PAQ.
    *** I'ts not easy to get a LV_ITEM back from another process.  There is a complicated way to do it in C++...

tstrob, Please DO NOT accept this comment as an answer.
EXPERTS: Post a comment if you are certain that an expert deserves credit.  Explain why.
DanRollins -- EE database cleanup volunteer
Per recommendation, points refunded and question closed.

CS Moderator