How to Add List-View Items and Subitems
This topic demonstrates how to add items and subitems to a list-view control.
What you need to know
Technologies
Prerequisites
- C/C++
- Windows User Interface Programming
Instructions
To add an item to a list-view control, an application must first define an LVITEM structure and then send an LVM_INSERTITEM message, specifying the address of the LVITEM structure. If an application uses report view, subitem text must be provided.
The following C++ code example fills an LVITEM structure and adds the list-view items by using the LVM_INSERTITEM message or the corresponding macro ListView_InsertItem. Because the application saves its own text, it specifies the LPSTR_TEXTCALLBACK value for the pszText member of the LVITEM structure. Specifying the LPSTR_TEXTCALLBACK value causes the control to send an LVN_GETDISPINFO notification code to its owner window whenever it needs to display an item.
// InsertListViewItems: Inserts items into a list view.
// hWndListView: Handle to the list-view control.
// cItems: Number of items to insert.
// Returns TRUE if successful, and FALSE otherwise.
BOOL InsertListViewItems(HWND hWndListView, int cItems)
{
LVITEM lvI;
// Initialize LVITEM members that are common to all items.
lvI.pszText = LPSTR_TEXTCALLBACK; // Sends an LVN_GETDISPINFO message.
lvI.mask = LVIF_TEXT | LVIF_IMAGE |LVIF_STATE;
lvI.stateMask = 0;
lvI.iSubItem = 0;
lvI.state = 0;
// Initialize LVITEM members that are different for each item.
for (int index = 0; index < cItems; index++)
{
lvI.iItem = index;
lvI.iImage = index;
// Insert items into the list.
if (ListView_InsertItem(hWndListView, &lvI) == -1)
return FALSE;
}
return TRUE;
}
// HandleWM_NOTIFY - Handles the LVN_GETDISPINFO notification code that is
// sent in a WM_NOTIFY to the list view parent window. The function
// provides display strings for list view items and subitems.
//
// lParam - The LPARAM parameter passed with the WM_NOTIFY message.
// rgPetInfo - A global array of structures, defined as follows:
// struct PETINFO
// {
// TCHAR szKind[10];
// TCHAR szBreed[50];
// TCHAR szPrice[20];
// };
//
// PETINFO rgPetInfo[ ] =
// {
// {TEXT("Dog"), TEXT("Poodle"), TEXT("$300.00")},
// {TEXT("Cat"), TEXT("Siamese"), TEXT("$100.00")},
// {TEXT("Fish"), TEXT("Angel Fish"), TEXT("$10.00")},
// {TEXT("Bird"), TEXT("Parakeet"), TEXT("$5.00")},
// {TEXT("Toad"), TEXT("Woodhouse"), TEXT("$0.25")},
// };
//
void HandleWM_NOTIFY(LPARAM lParam)
{
NMLVDISPINFO* plvdi;
switch (((LPNMHDR) lParam)->code)
{
case LVN_GETDISPINFO:
plvdi = (NMLVDISPINFO*)lParam;
switch (plvdi->item.iSubItem)
{
case 0:
plvdi->item.pszText = rgPetInfo[plvdi->item.iItem].szKind;
break;
case 1:
plvdi->item.pszText = rgPetInfo[plvdi->item.iItem].szBreed;
break;
case 2:
plvdi->item.pszText = rgPetInfo[plvdi->item.iItem].szPrice;
break;
default:
break;
}
break;
}
// NOTE: In addition to setting pszText to point to the item text, you could
// copy the item text into pszText using StringCchCopy. For example:
//
// StringCchCopy(plvdi->item.pszText,
// plvdi->item.cchTextMax,
// rgPetInfo[plvdi->item.iItem].szKind);
return;
}
Related topics