How do I get the text out of a listview in report style into an array?
I want it to be something like this:
[[item1],[subitem1]],[[item1],[subitem1]],[[item1],[subitem1]],
Here is some of my code:

hWndListView = CreateWindowEx(
0L, 
WC_LISTVIEW, 
"",
 WS_VISIBLE | WS_CHILD | LVS_REPORT,
0, 21, 340, 190, hWnd,
(HMENU) ID_LISTVIEW, hInstance,
 NULL);
  ListView_SetExtendedListViewStyle ( hWndListView, LVS_EX_FULLROWSELECT );
 // Here we put the info on the Coulom headers
// this is not data, only name of each header we like
 memset(&LvCol,0,sizeof(LvCol)); // Reset Coluom
LvCol.mask=LVCF_TEXT|LVCF_WIDTH|LVCF_SUBITEM; // Type of mask
LvCol.cx=0x0;                                // width between each coloum
LvCol.pszText="Time";                     // First Header
 LvCol.cx=0x30;


// Inserting Couloms as much as we want
				SendMessage(hWndListView,LVM_INSERTCOLUMN,0,(LPARAM)&LvCol); // Insert the coloum

LvCol.cx=0x120;
LvCol.pszText="Item";
SendMessage(hWndListView,LVM_INSERTCOLUMN,2,(LPARAM)&LvCol);

Dani AI

Generated

— the missing piece is to iterate every row (use the listview item count) and every column (use the header column count), then read each subitem into a string/container. Also check the column insertion index you used — the second column should be index 1, not 2; inserting at the wrong index can make your subitem indexing look wrong.

A safe pattern (Unicode build) that collects every cell into a vector-of-rows looks like this:

int itemCount = ListView_GetItemCount(hWndListView);
HWND hHeader = ListView_GetHeader(hWndListView);
int colCount = Header_GetItemCount(hHeader);

std::vector<std::vector<std::wstring>> table;
table.reserve(itemCount);

for (int i = 0; i < itemCount; ++i) {
    std::vector<std::wstring> row;
    row.reserve(colCount);
    for (int j = 0; j < colCount; ++j) {
        wchar_t buf[512] = { 0 };
        LVITEMW lvi = {};
        lvi.iSubItem = j;
        lvi.pszText = buf;
        lvi.cchTextMax = sizeof(buf) / sizeof(buf[0]);
        SendMessageW(hWndListView, LVM_GETITEMTEXTW, (WPARAM)i, (LPARAM)&lvi);
        row.emplace_back(buf);
    }
    table.emplace_back(std::move(row));
}

Notes and troubleshooting:

  • If your project is ANSI, use LVITEMA and LVM_GETITEMTEXTA (or use TCHAR and the neutral macros).
  • Make the buffer large enough for your longest cell; if text is truncated, increase buf.
  • If itemCount is zero, confirm you actually inserted items (LVM_INSERTITEM/LVM_SETITEM) rather than only headers.
  • Prefer std::vector/std::wstring (or std::string) rather than manual new/delete to avoid leaks.
  • Once in a vector, you can format the output into your desired "[[item],[subitem]]" representation easily with a small loop and conversions to UTF-8 if needed.

I can get the information from the first item and and sub item in the listview, but I don't know how to get the information from the rest. This is what I've got so far.

char *text = new char[256];
char *text2 = new char[256];
ListView_GetItemText(hWndListView, 0, 0, text, 256);
ListView_GetItemText(hWndListView, 0, 1, text2, 256);
delete [] text;
delete [] text2;

I need a way to move to the next item until there aren't any more items.

Could someone please help me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.