Newline and multiline textbox in C#

K

Kimmo Laine

Hi,

i know this is a stupid question, but... how can i add text to separate
lines when i have a multiline textbox?

// This don´t work
myTextBox.Text = "Hello\nWorld!";


thx

Kimmo Laine
 
A

Andreas Håkansson

Kimmo,

A linebreak in Win/DOS made up of two characters (carriage return and
line feed) so you will need to pass \r\n or use the Environment.NewLine
contstant instead.

HTH,

//Andreas
 
A

Andreas Håkansson

LC,

Please note that this would require two explicit type casts and would
be slower than using the escape characters or NewLine constand.

HTH,

//Andreas
 
L

LC

Andreas:

Please note in my example code that (char)13 is a constant expression:

const char LF = (char)13; //is valid

.... and it is fully evaluated at compile-time, even if the expression is a
sub-expression of a larger expression that contains non-constant constructs.

C# Reference. 14.15
 
J

Jon Skeet [C# MVP]

LC said:
Please note in my example code that (char)13 is a constant expression:

const char LF = (char)13; //is valid

... and it is fully evaluated at compile-time, even if the expression is a
sub-expression of a larger expression that contains non-constant constructs.

Yes, the char itself is a constant. However, the concatenation is still
required at runtime, so it's still slower than writing

Console.WriteLine ("Hello\r\nWorld"); or even

Console.WriteLine ("Hello"+Constants.CRLF+"World");

where Constants is defined as (say)

public class Constants
{
public const string CRLF = "\r\n";
}

Using Environment.Newline *doesn't* remove the concatenation, of
course, because it's not a compile-time constant.
 

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