How to Show New Line command in text box?
Solution 1
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
Solution 2
For winforms application set
this.textBox1.Multiline = true;
and use "\r\n"
as
textBox1.Text = "First line \r\nSecond line";
Solution 3
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. :)
user777304
Updated on June 04, 2022Comments
-
user777304 about 2 years
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.