LB_GETITEMDATA & LB_SETITEMDATA

Started by
4 comments, last by Dave Hunt 18 years, 8 months ago
I found from an earlier post to use this to have a second value that is able to be selected for each item. I am having a little problem using it though. My functions work correctly but ONLY if when I pass in the LB_SETITEMDATA value, I create it with a pointed and do not delete it. Here is an example of how I am doing it.

i++;
char* test = new char[256];
wsprintf((LPSTR)test, "test%d", i);
listbox->AddItem(file, test);
//delete [] test;

I highly doubt that is a good method to do it, since not deleting a pointer could lead to problems (so I've been told), but I cannot find a work around. Is there any idea on how I could go about doing this? Example of how this works add an item to a listbox via function with 2 paramaters item and value. item gets added to the listbox. I use LB_FINDSTRINGEXACT to find the index, then I use SendMessage with LB_SETITEMDATA as the message and the WPARAM is the index and the LPARAM is the value (second paramater) Then in a function to retrieve the itemdata I just use SendMessage() with an LB_GETITEMDATA message and pass the selected item's index via the WPARAM. Like I said it works fine, but only if the value is a pointer and never deleted. Any ideas/thoughts would be appreciated. Thanks again.
Advertisement
The data you are passing is "user" data and is owned by you. It would not be appropriate for the list box to take ownership of the data. If you pass it a pointer, it is just going to store the pointer and never do anything with it. It doesn't know anything about your data so it can't do anything fancy like copying what it points to.

If you allocate it, you delete it. But only after you are done with the data. So, you can wait until you clear the list box and delete all the pointers at that time.
Thank you for your reply Dave, I was starting to begin that is what was going on, I am now trying to think of the best way to go about doing this. I must declare the variables outside of my local function or the data will not be able to be deleted. I am thinking about creating a vector to hold all the information I need, but I need to study the vector class a little better to know how it works, and how when an item is delete I can alter each item in the vector appropriately.
Actually, you could just use the pointer returned from LB_GETITEMDATA to do the delete. That way you don't have to keep the pointers around in global variables/structures.
I didn't know I could do that, nor do I know how. I am guessing it would be something like.

delete [] SendMessage(hWndList, LB_GETITEMDATA, (WPARAM)index, NULL);


Would that be the correct way?

Thank you so much for your help, I was about to abandon that method and try something else. I really appreciate it.
That should work. For error checking, you might want to assign the result of SendMessage to a local variable and make sure it's not NULL before deleting. The NULL wouldn't cause the delete to fail, but it might indicate that something has gone wrong. Not necessary, but might help with debugging.

This topic is closed to new replies.

Advertisement