Posted on 2004-11-11
Last Modified: 2013-12-03
In windows programming, there are ListBox (or ComboBox) control messages that are 'LB_SETITEMDATA' & 'LB_GETITEMDATA'.
These messages should be very useful while adding and retrieving the strings in ListBox control window. But I only found using 'SendMessage' to add and get strings from the ListBox control window in the books. I am not very clear the usages of these messages.

Could someone show me an example of how to use the ListBox control window messages 'LB_SETITEMDATA' & 'LB_GETITEMDATA' in windows programming?


Question by:yc_yc
    1 Comment
    LVL 48

    Accepted Solution

    Suppose you have listbox hList. The following code fragment associates string "String n" for each line in the lisbox:

    int n = SendMessage(hList, LB_GETCOUNT, 0, 0);

    for ( int i = 0; i < n; i++ )
        char* s = new char[10];
        sprintf(s, "String %d", i);
        SendMessage(hList, LB_SETITEMDATA, i, (LPARAM)s);

    Now each item in listbox has value associated with it. This value is pointer to memory allocated on heap. The following code fragment reads these values:

    int n = SendMessage(hList, LB_GETCOUNT, 0, 0);

    for ( int i = 0; i < n; i++ )
        char* s = (char*)SendMessage(hList,  LB_GETITEMDATA, i, 0);
        // do something with s

    Before list item is destroyed, strings should be released:

    int n = SendMessage(hList, LB_GETCOUNT, 0, 0);

    for ( int i = 0; i < n; i++ )
        char* s = (char*)SendMessage(hList, LB_GETITEMDATA, i, 0);
        delete[] s;

    Item data may be some number or pointer. Programmer responsibility is to set, retreive and correctly release (if necessary) this data. Control just keeps this data as 32-bit value for each item. You may decide to keep any data by this way. For example, this may be some structure which is shown in dialog when user double-clicks list item.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
    With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now