Elements and Subitems in a List-View Control

I want to use a List-View control to display LDAP search results in a "grid". I wrote some test codes to see how this works, but it doesn't display as I want. As far as I understand, each Item equivalent to a "row" (using the LVS_REPORT style), and Subitem equivalent to a column (for example, for each element I can display several subitems, each in a separate column on the same line).

Here's my test code currently set to create four columns, with one element and four sub-elements (corresponding to four columns). Two functions: one for creating columns and one for inserting elements.

 int CreateColumns(HWND *hwndlistbox) { wchar_t *cnames[100]; LVCOLUMN lvc; int i; cnames[0] = L"column1"; cnames[1] = L"column2"; cnames[2] = L"column3"; cnames[3] = L"column4"; cnames[4] = NULL; lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM; for (i = 0; cnames[i] != NULL; i++) { lvc.iSubItem = i; lvc.pszText = cnames[i]; lvc.cx = 100; lvc.fmt = LVCFMT_LEFT; ListView_InsertColumn(*hwndlistbox, i, &lvc); } return i; } void InsertItems(HWND *hwndlistbox, int *columncount) { LVITEM lvi; wchar_t *items[100]; int i, j; items[0] = L"text1"; items[1] = L"text2"; items[2] = L"text3"; items[3] = L"text4"; items[4] = NULL; lvi.mask = LVIF_TEXT; lvi.iItem = 0; for (i = 0; i < *columncount; i++) { lvi.pszText = items[i]; lvi.iSubItem = i; ListView_InsertItem(*hwndlistbox, &lvi); } } 

I expect this to create one row ( lvi.iItem = 0; ) with a text line under each column ( lvi.iSubItem = i; ). Instead, the following will be displayed:

enter image description here

Changing lvi.iSubItem = i to lvi.iSubItem = 0 causes each text line to appear in a new line in the first column:

enter image description here

I played with it, hard-coded numbers to iItem and iSubItem , changing both to i , but I can't get it to display text anywhere except the first column. What am I doing wrong?

+2
source share
2 answers

First of all, cnames and items arrays are declared as an array of pointers, but you do not allocate memory for them; you need to declare them as an array of strings, for example wchar_t cnames[100][40]; .

Secondly, you need to use ListView_InsertItem to insert the item and set the value for the first column, then use ListView_SetItem to add additional columns, e.g.

 lvi.pszText = items[0]; lvi.iSubItem = 0; ListView_InsertItem(*hwndlistbox, &lvi); for (i = 1; i < *columncount; i++) { lvi.pszText = items[i]; lvi.iSubItem = i; ListView_SetItem(*hwndlistbox, &lvi); } 
+4
source

Each row shows one element, so you cannot fill columns by adding elements.

As the documentation says :

"You cannot use ListView_InsertItem or LVM_INSERTITEM to insert LVM_INSERTITEM . The iSubItem structure must be zero. See LVM_SETITEM for information on setting the subitems."

The LVM_SETITEM documentation explains how to set subitem text.

+2
source

Source: https://habr.com/ru/post/986285/


All Articles