I'm want to use a List-View
control to display results of an LDAP search in a "grid". I've written some test code to see how it works, but it's not being displayed as I want. As I understand it, each Item
is equivalent to a "row" (using LVS_REPORT
style), and the Subitem
is equivalent to a "column" (eg for each item I can display a number of subitems, each in a separate column on the same row).
Here's my test code, currently set to create four columns, with a single Item and four Subitems (corresponding to the four columns). Two functions: one to create the columns, the other to insert items.
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 generate a single row ( lvi.iItem = 0;
) with a text string under each column ( lvi.iSubItem = i;
). This is what it displays instead:
Changing lvi.iSubItem = i
to lvi.iSubItem = 0
results in each text string being displayed as a new row in the first column:
I've played around with it, hardcoding the numbers on both iItem
and iSubItem
, changing both to i
, but I can't get it to display the text anywhere other than the first column. What am I doing wrong?
First of all, your cnames
and items
arrays are declared as array of pointers, but you are not allocating memory for them; you would need to declare them as an array of strings, like wchar_t cnames[100][40];
.
Secondly, you need to use ListView_InsertItem
to insert an item and set the value for the first column, then use ListView_SetItem to add additional columns, like
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);
}
Each row shows a single item so you cannot populate the columns by adding items.
"You cannot use ListView_InsertItem
or LVM_INSERTITEM
to insert subitems. The iSubItem
member of the LVITEM
structure must be zero. See LVM_SETITEM
for information on setting subitems."
The LVM_SETITEM documentation explains how to set the text of a sub-item.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.