Skip to main content
GameDev.net gamedev.net
🔒 Locked

dump list box into a string

Started by localrob2 Jan 15, 2007 at 2:51 PM 8 replies 1.1k views
Original Post
localrob2
localrob2
I'm working with the win api listbox. Ultimately I'm trying to make a playlist creator, where I can select mp3s from one list box, add them to another, then write the 2nd list box to a text file. My question is, how do I dump the contents of a list box to a string? I don't have much experience using the windows api, so maybe I don't even want it dumped into a string, I'm not sure. Should I be adding the selected content from list box to an array each time it's selected, then printing the array?
Colin Jeanne
Colin Jeanne
For each element in the list box you'll need to write that to your file. There is no "dump all data to a string" message you can send.
localrob2
localrob2
Alright, but how do I do that?
I'm using SendMessage() with LB_ADDSTRING to add strings to the listbox, but I don't know how to take that element and write it to a file. Am I way in over my head here?
raz0r
raz0r
Quote:
Original post by localrob2
Alright, but how do I do that?

LB_GETCOUNT, LB_GETTEXTLEN, LB_GETTEXT.

localrob2
localrob2
Yes I am able to get an element from the listbox using LB_GETTEXT, it's an array of TCHARs, but I can write that to the file. Thanks.

Quote:
Write that data you're sending in SendMessage() to a "database"?

Isn't that kind of what's happening? I'm sending the message from one database to another. The database being a standard control listbox. I just didn't understand how I could grab and interpret the elements in the listbox.
AnthonyG
AnthonyG
Here you go :), I'm sure you know what headers to include(As I am too lazy to type directives at the moment).

I believe this is what you're looking for.

As far as I know, It's impossible to grab every item and stuff it into a string. What this function does, Is loop X times where X is the number of items in the listbox, and in each loop it writes the item data to a text file, Then moves downward until Lopp #==X

void DumpListboxToFile(char filename[],HWND listbox){  char LBItemBuffer[300];  FILE *file=fopen(filename,"a+");    for(int tick=0;tick<SendMessage(listbox,LB_GETCOUNT,0,0);tick++)  {    SendMessage(listbox,LB_GETTEXT,(WPARAM)tick,(LPARAM)LBItemBuffer);    fprintf(file,"%s\n",LBItemBuffer);  }  fprintf(file,"---------------------------------------\n");  fclose(file);}


Yep, That should work.

[Edited by - AnthonyG on January 16, 2007 6:23:05 PM]
-AnthonyG
raz0r
raz0r
Quote:
Original post by AnthonyG
As far as I know, It's impossible to grab every item and stuff it into a string.
Why would it be impossible? Other then that, there are some issues with the code that you've posted.
AnthonyG
AnthonyG
Forgive the grammar. I mean it's impossible in one function call, I.E. SendMessage(listbox,LB_GRABITALL,0,(LPARAM)Buffer);

Thank you for noticing the error, I fixed it :)
-AnthonyG
localrob2
localrob2
Thank you all for the help, this is what I ended up doing.

for(int j = 0;j<NumEntries;j++)     {	//get the length of the indexed element			LengthString = SendMessage(playList.hCntrl,LB_GETTEXTLEN,j,0);	//get the text of the element and put in tchBuffer	SendMessage(playList.hCntrl,LB_GETTEXT,j,(LPARAM) tchBuffer);	for(int i = 0;i<LengthString;i++)         	List << char(tchBuffer);	List << "\n\n";     }//no more entries in listbox


it is not the most efficient way, but it works

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.