Jump to content
  • Advertisement
Sign in to follow this  
GetWindowRect

LB_GETITEMDATA & LB_SETITEMDATA

This topic is 4842 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!