Discussion:
add items to a list view
(too old to reply)
Larry
2010-02-13 23:53:58 UTC
Permalink
Hi,

I am using the following code to create a List View Control:

////////////////////////////////////////////

// Adding List-View Columns
TCHAR szText[256] = {0};
TCHAR szString[5][20] = { TEXT("Column 1"),TEXT("Column 2"),TEXT("Column
3") };

HWND hlistview = CreateWindowEx(0, WC_LISTVIEW, 0, WS_VISIBLE | WS_CHILD |
WS_BORDER | LVS_ALIGNLEFT | LVS_REPORT | LVS_EDITLABELS, 40,90,430,200,
hwnd, (HMENU)IDC_LIST1, hInst, NULL);
SetWindowFont(hlistview,hfont0,TRUE);

LVCOLUMN lvc;
ListView_DeleteAllItems(hlistview);

lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;

for (int k = 0; k < 3; k++)
{
lvc.iSubItem = k;
lvc.cx = 100;
lvc.pszText = szString[k];
lvc.fmt = LVCFMT_LEFT;

ListView_InsertColumn(hlistview, k, &lvc);
}

// Adding List-View Items and Subitems

struct petinfo
{
TCHAR szKind[10];
TCHAR szBreed[50];
TCHAR szPrice[20];
};

petinfo pi[] =
{
{TEXT("Dog"), TEXT("Poodle"), TEXT("$300.00")},
{TEXT("Cat"), TEXT("Siamese"), TEXT("$100.00")},
{TEXT("Fish"), TEXT("Angel Fish"), TEXT("$10.00")}
};

LVITEM lvi;
lvi.mask = LVIF_TEXT | LVIF_PARAM | LVIF_STATE;
lvi.state = 0;
lvi.stateMask = 0;

for (int k = 0; k < 3; k++)
{
lvi.iItem = k;
lvi.iSubItem = 0;
lvi.lParam = (LPARAM) &pi[k];
int res = ListView_InsertItem(hlistview, &lvi);
}
////////////////////////////////////////////////////

while I get the list view collumn correct I don't get any items...what am I
doing wrong?

thanks
Igor Tandetnik
2010-02-14 00:04:31 UTC
Permalink
Post by Larry
LVITEM lvi;
lvi.mask = LVIF_TEXT | LVIF_PARAM | LVIF_STATE;
lvi.state = 0;
lvi.stateMask = 0;
for (int k = 0; k < 3; k++)
{
lvi.iItem = k;
lvi.iSubItem = 0;
lvi.lParam = (LPARAM) &pi[k];
int res = ListView_InsertItem(hlistview, &lvi);
}
////////////////////////////////////////////////////
while I get the list view collumn correct I don't get any items...what am I
doing wrong?
lvi.mask claims you are setting text, but you never assign lvi.pszText
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
Larry
2010-02-14 00:22:27 UTC
Permalink
Post by Igor Tandetnik
lvi.mask claims you are setting text, but you never assign lvi.pszText
Well, I thought I was with this:

lvi.lParam = (LPARAM) &pi[k];

Also, by passing this:

lvi.pszText = pi[k];

I get this:

error C2440: '=' : cannot convert from 'petinfo' to 'LPWSTR'
Igor Tandetnik
2010-02-14 00:56:45 UTC
Permalink
Post by Larry
Post by Igor Tandetnik
lvi.mask claims you are setting text, but you never assign lvi.pszText
lvi.lParam = (LPARAM) &pi[k];
This assigns to lParam (which you promised with LVIF_PARAM), not to pszText (which you promised with LVIF_TEXT but are not doing).

Perhaps you plan to use a callback for retrieving item text - in this case, set pszText to LPSTR_TEXTCALLBACK and handle LVN_GETDISPINFO notification.
Post by Larry
lvi.pszText = pi[k];
error C2440: '=' : cannot convert from 'petinfo' to 'LPWSTR'
pszText is supposed to point to a string of text, not to some arbitrary structure.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
Continue reading on narkive:
Loading...