HOWTO: Associate Data with a List Box EntryID: Q74345
|
In the Microsoft Windows graphical environment, an application can use the LB_SETITEMDATA and LB_GETITEMDATA messages to associate additional information with each entry in a list box. These messages enable an application to associate an arbitrary LONG value with each entry and to retrieve that value. This article documents how an application uses these messages.
In this example, the application will associate a 64-byte block of data
with each list box entry. This is accomplished by allocating a global
memory block and using the LB_SETITEMDATA message to associate the handle
of the memory block with the appropriate list box item.
During list box initialization, the following code is executed for each
list box item:
if ((hLBData = GlobalAlloc(GMEM_MOVEABLE, 64)))
{
if ((lpLBData = GlobalLock(hLBData)))
{
// Store data in 64-byte block.
GlobalUnlock(hLBData);
}
}
// NOTE: The MAKELONG is not needed on 32-bit platforms.
SendMessage(hListBox, LB_SETITEMDATA, nIndex, MAKELONG(hLBData, 0));
// NOTE: The return from LB_GETITEMDATA is a long on 32-bit platforms.
if ((hLBData = LOWORD(SendMessage(hListBox, LB_GETITEMDATA,
nIndex, 0L))))
{
if ((lpLBData = GlobalLock(hLBData)))
{
// Access or manipulate the data or both.
GlobalUnlock(hLBData);
}
}
if ((hLBData = LOWORD(SendMessage(hListBox, LB_GETITEMDATA,
nIndex, 0L))))
GlobalFree(hLBData);
Additional query words: combobox listbox
Keywords : kbCtrl kbListBox kbNTOS kbGrpUser kbWinOS
Version :
Platform :
Issue type : kbhowto
Last Reviewed: March 7, 1999