Associating Data with a List Box Entry

Last reviewed: November 2, 1995
Article ID: Q74345
The information in this article applies to:
  • Microsoft Windows Software Development Kit (SDK) versions 3.0 and 3.1
  • Microsoft Win32 Application Programming Interface (API) included with:

        - Microsoft Windows NT versions 3.5 and 3.51
        - Microsoft Windows 95 version 4.0
    

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 reference words: 3.00 3.10 3.50 4.00 95 combobox listbox
KBCategory: kbui
KBSubcategory: UsrCtl


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: November 2, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.