Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi,

I've got a series of case statements which determine the output of each line in a non editable textbox. It's pretty complicated and messy so ive done an example instead below

My first idea was to just put each case as a string :

C++
<pre lang="text">switch(x)
case(1): str1 = "hello";
case(2): str2 = "hi";
case(3): str3 = "bonjour";

The problem is some of my case statements have two outputs while others have one... ie case(2) gives two strings out while case(1) and case(3) output one string.

Now I know I could add these into one string - but I dont want to. I need these on seperate lines in a textbox.

I was using
C++
this->textBox1->Text = "Greeting: " + str1 + Environment::NewLine + 
// ... continues to next case table string outputs

But as I said if I have two outputs for some cases and one for others I cant use this...

So my overall question is how can I set a new line for a textbox without overwriting the current text? I know you can append strings, but how do you do it so it stores the newline between substrings?

Many thanks
Posted

First, of all, you cannot "add to a string", ever. String objects are immutable. In contrast to arrays, you cannot even write a different character in the position which is already available. All operations on strings, such as concatenation, in fact simply create a brand new string object and copy "old" data to it. If you assign the result of such operation to the same variable, a brand new string object is created first, data is copied, and eventually abandoned (unreachable) "old" string object will be garbage-collected.

If you track all what happens, you will understand that repeated concatenation, such as expressions with several '+' suffer from poor performance, which can be a considerable factor when, for example, you try to do it in loop. Right approach to it is using the mutable type, System.Text.StringBuilder. String.Format also efficient and should always be used instead of an expression with multiple '+'. Please see:
http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx[^],
http://msdn.microsoft.com/en-us/library/system.string.aspx[^],
http://msdn.microsoft.com/en-us/library/b1csw23d.aspx[^].

Now, I'm not sure you really understand the nature of the "set a new line". In fact, string with multiple lines is still a single string. The lines are just separated with one or more characters, "line separator":
http://en.wikipedia.org/wiki/Newline[^].

As you can see, the line separator depends on the platform. To use it correctly, you should always use System.Environment.NewLine:
http://msdn.microsoft.com/en-us/library/system.environment.newline.aspx[^].

Finally, the simple text boxes (not RTF) don't have the operations appending a line. You always should rewrite the whole Text. If you really want to append, use ListBox.

—SA
 
Share this answer
 
Comments
Menon Santosh 23-Mar-13 5:43am    
Nicely Explained
Sergey Alexandrovich Kryukov 23-Mar-13 19:30pm    
Thank you, Menon.
—SA
I'd just embed \r\n in the text; forget NewLine, it's pointless and only causes problems like yours.
 
Share this answer
 
To anybody as tired as me, answer is:

this->textBox1->Text += strA + Environment::NewLine;
this->textBox1->Text += strB + Environment::NewLine;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900