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?


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.