Jump to content
  • Advertisement
Sign in to follow this  
josh1billion

Win32 API listboxes - LB_SETITEMDATA and LB_GETITEMDATA

This topic is 4179 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'm trying to use a listbox in the Win32 API. I'm trying to associate a string with each listbox item, by sending the name through LB_SETITEMDATA, and then getting the selected listbox entry's string back later on with LB_GETITEMDATA. I must be doing something wrong here, because I tried this and, to test it, I had a MessageBox display the string retrieved from LB_GETITEMDATA and it was blank. Below are code snippets from both message calls. I tried searching the net for examples of LB_SETITEMDATA/LB_GETITEMDATA usage, but there seems to be hardly anything out there (and half of it's in another language-- be it another programming language or even another spoken language). Anyway, here are code snippets of how I'm sending the two messages. NOTE: You can see that I checked for LB_ERR returns-- these returns were never triggered, so it must not be a problem with sending the messages: LB_SETITEMDATA - to tell the listbox entry what the string is that should be associated with it
		int id = SendDlgItemMessage(hDlg, IDC_LISTBOX, LB_ADDSTRING, 0, (LPARAM)buffer.c_str()); // add entry to listbox
		char* temp = new char[]; // this will be sent in 
		strcpy(temp, tdCurrent->strFolderName.c_str()); // get the string ready.. now it will have some text, like "001" or "002"
		int iReturn = SendMessage(hDlg, LB_SETITEMDATA, id, (WPARAM)temp); // send in the string
		if (iReturn == LB_ERR)
			MessageBox(hDlg, "Warning: LB_SETITEMDATA message returned an LB_ERR", "Warning", MB_OK);
LB_GETITEMDATA - retrieves the listbox entry's associated data (a string)
			char* strSelectedFolderName = NULL; // this is where the associated string will be stored
			int iSelectedID = SendDlgItemMessage(hDlg, IDC_LISTBOX, LB_GETCURSEL, 0, 0); // first, find the index of the selected listbox entry
			int iResult = SendDlgItemMessage(hDlg, IDC_LISTBOX, LB_GETITEMDATA, iSelectedID, (LPARAM)strSelectedFolderName);
			if (iResult == LB_ERR)
				MessageBox(hDlg, "Error in LB_GETITEMDATA", "Error", MB_OK);
			MessageBox(hDlg, strSelectedFolderName, "The stored data for the newly selected item is...", MB_OK);
So the end result is that strSelectedFolderName is a blank string, unfortunately. Instead, it's SUPPOSED to contain the text sent in earlier with the LB_SETITEMDATA message.

Share this post


Link to post
Share on other sites
Advertisement
Your use of LB_GETITEMDATA is incorrect. The LPARAM parameter is ignored and the item data is the return value of the message. It should look like this:

int iResult = SendDlgItemMessage(hDlg, IDC_LISTBOX, LB_GETITEMDATA, iSelectedID, 0);

if (iResult == LB_ERR)
{
MessageBox(hDlg, "Error in LB_GETITEMDATA", "Error", MB_OK);
}

MessageBox (hDlg, (char *) iResult, "The stored data for the newly selected item is...", MB_OK);


Note that it is not possible to store a value of LB_ERR as you won't be able to determine if there's been an error. If you are just storing pointers then this will not be a problem.

Skizz

Share this post


Link to post
Share on other sites
Skizz is correct about the proper usage of LB_GETITEMDATA. You have another error, though, when setting the data:

int iReturn = SendMessage(hDlg, LB_SETITEMDATA, id, (WPARAM)temp);

Should be:

int iReturn = SendDlgItemMessage(hDlg, IDC_LISTBOX, LB_SETITEMDATA, id, (WPARAM)temp);

Share this post


Link to post
Share on other sites
Ahhh.. don't you hate it when you make a little mistake like that and it causes [here, one half of] a huge bug?

You're both right, and it's working perfectly now. Thanks much you guys! :)

Share this post


Link to post
Share on other sites
And it should really be:

int iReturn = SendDlgItemMessage(hDlg, IDC_LISTBOX, LB_SETITEMDATA, id, (LPARAM)temp);

for compatability.

Skizz

Share this post


Link to post
Share on other sites
Just another addon:


char* temp = new char[]; // this will be sent in
strcpy(temp, tdCurrent->strFolderName.c_str()); // get the string ready.. now it will have some text, like "001" or "002"


Shouldn't there be a size in the new?

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!