Search code examples
c++listviewwinapilistviewitemwin32gui

Iterating over ListView Items win32 api


I currently have a handle to my Listview via HWND lv = GetDlgItem(hDlg, MY_LISTVIEW)

and its currently populated with items using ListView_SetItemText(); I want to update each item in that listview based on data that has been updated externally. How would I iterate over each listview item given my handle?


Solution

  • ListViews use a 0-based index to identify items, so to iterate over the items it's simply a matter of getting the total and then running a loop that counts from 0. For example,

    int iNumItems = ListView_GetItemCount(lv);
    for (int iIndex = 0; iIndex < iNumItems; ++iIndex)
    {
        // update this item
        ListView_SetItemText(lv, iIndex, 0, ...);
    }