Microsoft KB Archive/74345

{|
 * width="100%"|

HOWTO: Associate Data with a List Box Entry

 * }

Q74345

-

The information in this article applies to:


 * Microsoft Win32 Software Development Kit (SDK)
 * Microsoft Windows Software Development Kit (SDK) versions 3.0, 3.1

-

SUMMARY
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.

MORE INFORMATION
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)); To retrieve the information associated with a list box entry, the following code can be used:

// 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); }     } Before the application terminates, it must free the memory associated with each list box item. The following code frees the memory associated with one list box item:

  if ((hLBData = LOWORD(SendMessage(hListBox, LB_GETITEMDATA, nIndex, 0L)))) GlobalFree(hLBData); These techniques can be used to associated data with an entry in a combo box by substituting the CB_SETITEMDATA and CB_GETITEMDATA messages.

Additional query words: combobox listbox

Keywords : kbCtrl kbListBox kbOSWinNT kbOSWin2000 kbSDKWin32 kbGrpDSUser kbOSWin

Issue type : kbhowto

Technology : kbWin32SDKSearch kbAudDeveloper kbWin3xSearch kbSDKSearch kbWin32sSearch kbWinSDKSearch kbWinSDK300 kbWinSDK310