Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Adding Strings To A Listbox From A Multiline TextBox.

As the title suggests i am having some problems with adding strings to a Listbox from a multi-line textbox. So i'm looping through the lines in the textbox using EM_GETLINE and adding to the ListBox using LB_ADDSTRING etc.. This is not the problem, my problem is with the string it's-self. Do i have to create a new string buffer for each iteration of the loop to allow for different lengths of strings in the TextBox? Suggestions are welcomed and code snippets are more than welcomed!! :)

I know this is a bit of a newbie question. But we all have to start somewhere :D
[590 byte] By [zeek840] at [2007-11-11 10:25:57]
# 1 Re: Adding Strings To A Listbox From A Multiline TextBox.
I suggest you create only one buffer which is big enough to hold any line of text (255 bytes). You'll have some wasted space here but who cares. An alternative would be to also process EM_LINELEGTH message to know exactly the size of the buffer for the line. Anyway I think that pretty soon you'll switch to some kind of C++ classes and then you usually don't need to process those win messages to get it done.
Ivan** at 2007-11-11 20:58:57 >
# 2 Re: Adding Strings To A Listbox From A Multiline TextBox.
By C++ classes are you talking about MFC?
zeek840 at 2007-11-11 20:59:51 >
# 3 Re: Adding Strings To A Listbox From A Multiline TextBox.
Also, the problem isn't assigning the buffer for the string. Its that when a line is longer in the textbox than the one below it there seems to be some overflow?
zeek840 at 2007-11-11 21:00:56 >
# 4 Re: Adding Strings To A Listbox From A Multiline TextBox.
MFC or something else, I'm using VCL which is very similar to VB programming.
Post your code but I meant if you make the buffer large enough to accomodate for any line length then you really shouldn't worry about it overflowing
Ivan** at 2007-11-11 21:01:51 >
# 5 Re: Adding Strings To A Listbox From A Multiline TextBox.
TCHAR lstboxstr[255];

// Count previously defined as the number of lines in the textbox

for (i=0 ; i < count ; i++)

{


SendMessage(txtbox, EM_GETLINE, i, (LPARAM) &lstboxstr);


SendMessage(lstBox, LB_ADDSTRING, i, (LPARAM) lstboxstr);
}

Fist of all don't have a go about the coding i just testing stuff. But anyway, the loop begins with lstboxstr has a buffer of 255. But there seems to be some foreign characters appear and the text is never as it appears in the textbox, there are always bits added to it.
zeek840 at 2007-11-11 21:02:55 >
# 6 Re: Adding Strings To A Listbox From A Multiline TextBox.
TCHAR lstboxstr[255];

// Count previously defined as the number of lines in the textbox

for (i=0 ; i < count ; i++)

{

*((WORD*)lstboxstr) = 255; //FIRST SET THE FIRST WORD OF THE BUFFER TO 255; THIS WILL BE OVERWRITTEN BY SENDMESSAGE
DWORD charcopied = SendMessage(txtbox, EM_GETLINE, i, (LPARAM) lstboxstr);
lstboxstr[charcopied] = '\0'; //null terminate the string


SendMessage(lstBox, LB_ADDSTRING, 0, (LPARAM) lstboxstr); //wparam isn't used
}

I haven't tried it but it should be something like that
Ivan** at 2007-11-11 21:03:55 >
# 7 Re: Adding Strings To A Listbox From A Multiline TextBox.
Thanks that works just fine. Could you just explain one part to me...

*((WORD*)lstboxstr) = 255;

Am i right in saying that you are creating a pointer of the type WORD and casting lstboxstr to that type? But why the asterisk before the opening bracket?
zeek840 at 2007-11-11 21:04:59 >
# 8 Re: Adding Strings To A Listbox From A Multiline TextBox.
You're partuially correct. The cast expression forces the compiler to treat lstboxstr (which is a pointer to char, actually) to a pointer to DWORD. Then, the extra asterisk dereferences the resulting pointer, which enables the program to assign the value 255 to the DWORD. Actually, the first four bytes of lstboxstr are treated as a DWORD.
Danny at 2007-11-11 21:05:56 >
# 9 Re: Adding Strings To A Listbox From A Multiline TextBox.
That line of code isn't needed, at least not on my system. Danny is right but it's WORD not DWORD
Ivan** at 2007-11-11 21:07:00 >