TextBox and New Line

  • Thread starter Thread starter poojo hackma
  • Start date Start date
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.
 
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.
 
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
 
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);
 
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);
 
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.
 
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:
 
Back
Top