user777304
user777304

Reputation: 455

How to Show New Line command in text box?

Hi i am doing a small project in C#, and i need to know what commands are comming from input source so i can do my work accordingly.. here is example...

textBox1.Text = "First line \nSecond line";
richTextBox1.Text = "First line \nSecond line";

Richtextbox shows this output:

First line

Second line

Textbox show this output:

First line Second line

please tell me how to show new line "\n" or return "\r" or similar input as a character output in text or richtextbox. so i can know that newline command is coming from input data.

for example text or richtext box will show this output.

First line \nSecond line

thankx in advance.

Upvotes: 5

Views: 16447

Answers (6)

New Developer
New Developer

Reputation: 23

TextBoxt1.Text ="FirstLine \r\n SecondLine";

Upvotes: 1

Mohit
Mohit

Reputation: 11314

This is the Image how your text box will appear Change Your text box property from single line to Multiline then it will change to new line

TextBox1.TextMode = TextBoxMode.MultiLine;
        TextBox1.Text = "First line \n New line ";

Upvotes: 0

ABH
ABH

Reputation: 3439

For winforms application set

this.textBox1.Multiline = true;

and use "\r\n" as

textBox1.Text = "First line \r\nSecond line";

Upvotes: 3

Justin Pihony
Justin Pihony

Reputation: 67075

You want to either prefix your string with @ or you can use a double slash before each n (\n). Both of these are ways of escaping the \ so that it displays instead of being treated as part of a new line.

 @"This will show verbatim\n";
 "This will show verbatim\\n";

You can utilize this by performing a Replace on your incoming text

richTextBox1.Text = richTextBox1.Text.Replace("\n", "\n\\n");
richTextBox1.Text = richTextBox1.Text.Replace("\r\n", "\r\n\\n");

In the replace, I left the original linebreak so that it will be there, just followed by the displaying version. You can take those out if you dont want that. :)

Upvotes: 1

Dhananjay
Dhananjay

Reputation: 3793

Lets say that i have a string which have new line :

 string st = "my name is DK" + Environment.NewLine + "Also that means it's my name";

Now that i want to show that there is new line in my text there you go :

 textBox1.Text = st.Replace(Environment.NewLine, "%");

This will show the newline chat with % sign

Upvotes: 5

YoryeNathan
YoryeNathan

Reputation: 14522

Use the combination "\r\n" (or Environment.NewLine constant which contains just that).

Upvotes: 0

Related Questions