TextBox and New Line

P

poojo hackma

How is the New Line character written to the TextBox field
(TextBox.Multiline=True)?

For example:

TextBox1.Text = "Line 1\nLine 2";

Above does not produce 2 lines in the TextBox1.

Simple question, but I am stuck on this. Thanks for your help.
 
N

Nicholas Paldino [.NET/C# MVP]

poojo,

You need to add the newline/carriage return character sequence, like so:

TextBox1.Text = "Line 1\r\nLine 2";

I prefer to use what is returned by the static NewLine property on the
Environment class (when using the string for display purposes):

TextBox1.Text = string.Format("Line 1{0}Line 2", Environment.NewLine);

Hope this helps.
 
Q

Q. John Chen

In Windows, there should be a CR (Carrage Return) and an LF (Line
Feed). So, do the following:

TextBox1.Text = "Line 1\r\nLine 2";

or

TextBox1.Text = "Line1" + System.Environment.NewLine + "Line2";


John
 
D

Dave Sexton

Just FYI,

Don't do:

TextBox1.Text = "Line1" + System.Environment.NewLine + "Line2";

Do:

TextBox1.Text = string.Concat("Line 1", Environment.NewLine, "Line 2");

or

TextBox.Text = string.Format("Line 1{0}Line 2", Environment.NewLine);
 
Q

Q. John Chen

Agree.

Dave said:
Just FYI,

Don't do:

TextBox1.Text = "Line1" + System.Environment.NewLine + "Line2";

Do:

TextBox1.Text = string.Concat("Line 1", Environment.NewLine, "Line 2");

or

TextBox.Text = string.Format("Line 1{0}Line 2", Environment.NewLine);
 
N

Nicholas Paldino [.NET/C# MVP]

You would actually both be wrong then.

The line:

TextBox1.Text = "Line1" + System.Environment.NewLine + "Line2";

Compiles to:

TextBox1.Text = string.Concat("Line 1", Environment.NewLine, "Line 2");

There is no difference in what they actually do. The only reasons would
be for semantic/aesthetic reasons.

The last one:

TextBox.Text = string.Format("Line 1{0}Line 2", Environment.NewLine);

Is actually slower, since the code has to parse the string in order to
find the tokens.

Of course, this is assuming that you know what is being concatenated.
If you were iterating through a loop of items, and you didn't know what you
were concatenating, then StringBuilder is your best bet.
 
Q

Q. John Chen

I always thought that string.Format or Concat does a better job
internally (without re-allocate the string buffer) than the '+'. Thanks
for clearing this for me.


You would actually both be wrong then.

The line:

TextBox1.Text = "Line1" + System.Environment.NewLine + "Line2";

Compiles to:

TextBox1.Text = string.Concat("Line 1", Environment.NewLine, "Line 2");

There is no difference in what they actually do. The only reasons would
be for semantic/aesthetic reasons.

The last one:

TextBox.Text = string.Format("Line 1{0}Line 2", Environment.NewLine);

Is actually slower, since the code has to parse the string in order to
find the tokens.

Of course, this is assuming that you know what is being concatenated.
If you were iterating through a loop of items, and you didn't know what you
were concatenating, then StringBuilder is your best bet.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Q. John Chen said:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top