21
votes

I have a textbox, and I'm trying to print to it with the following line of code:

logfiletextbox.Text = logfiletextbox.Text + "\n\n\n\n\n" + o + " copied to " + folderlabel2.Text;

Where folderlabel 2 is obviously a textbox. The first thing I've put in is the same textbox, so that no text is erased. The excessive new lines have proven my problem, because there are no new lines in the textbox (yes, set to multiline). The "o" is of type FileInfo in a FileInfo array.

Why won't these newlines show up in the text box?

6
You need to use CRLF pairs when inserting into a textbox \r\n. - Bill
In case of UWP the TextBox has to be set explicitly to a mode were it accepts multiple lines AcceptsReturn="True" or TextWrapping="Wrap" it doesn't care about "\r\n" vs "\n". - Eelke

6 Answers

47
votes

Use "\r\n" instead of "\n". Windows text boxes need CRLF as line terminators, not just LF.

Potentially you could use Environment.NewLine instead - but I don't know what Mono TextBoxes do in terms of working with "\n" (which is what Environment.NewLine would be on a Linux box). If it starts putting extra stuff at the end if you use "\r\n" then that will break plenty of existing apps - but if it requires "\r\n" that would break apps which use Environment.NewLine.

Environment.NewLine is meant to be the default new line for the whole platform you're running on - but what if you're using a widget toolkit which does one thing, but text files typically do something else? Frankly it's a bit of a mess. It would be nice if there were a separate TextBox.NewLine property which different implementations could handle appropriately.

4
votes

I believe TextBoxes want an Environment.NewLine (which should be "\r\n")

Note that it must be the carriage return (\r) followed by the new line (\n). If you reverse the order, it won't work.

2
votes

A TextBox control expects a Carriage Return before your Line Feeds (0x0D 0x0A). Use "\r\n" or System.Environment.Newline.

2
votes

in stead of using \n we can use

Environment.NewLine

i hope it will help

0
votes

This is how i created append and new line for display txtitems.Text = txtitems.Text + Environment.NewLine + dr[0].ToString() +" "+dr[1].ToString();

0
votes

Anyone that is using VB.net, be on the lookout for vbCr

Here is an example:

return "My name is" & vbCr & "John" & vbCr & "Doe"