25

I'm developing a program that I'm using a string(generatedCode) that contains some \n to enter a new-line at the textBox that I'm using it(textBox1.Text = generatedCode), but when I'm running the program, instead of breaking that line I'm seeing a square.

Remember that I've set the Multiline value of the textBox to True.

Nathan Campos
  • 28,769
  • 59
  • 194
  • 300

4 Answers4

55

Replace \n with \r\n - that's how Windows controls represent newlines (but see note at bottom):

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

or

textBox1.Text = generatedCode.Replace("\n", Environment.NewLine);

Note: As discussed in comments, you may want to use Environment.NewLine. It's unclear though - it's not well-defined what line separator Windows Forms controls should use when they're not running on Windows. Should they use the platform default, or the Windows one (as it's a port of a Windows GUI control)? One of the examples in MSDN does use Environment.NewLine, but I've seen horribly wrong examples in MSDN before now, and the documentation just doesn't state which is should be.

In an ideal world, we'd just have one line separator - and even in a second best world, every situation would clearly define which line separator it was expecting...

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 2
    Basically the same thing, but it's probably safer to use Environment.NewLine instead of \r\n, I think. – jean Nov 17 '09 at 20:08
  • Hmm, question - ought "\r\n" be System.Environment.NewLine? I appreciate that they're the same (ish), its more of a sort of coding standardy type question – Murph Nov 17 '09 at 20:09
  • Given that ".NET" does not imply "Windows," I would strongly concur with using `System.Environment.NewLine`. – qid Nov 17 '09 at 20:12
  • That entirely depends on what other platforms decide to do for Windows Forms controls. Given that they're *Windows* Forms controls, they may well copy the use of "\r\n". To be honest, it feels like Mono is stuck between a rock and a hard place on this one. I would definitely use `Environment.NewLine` for writing a text file for example, but I'm not so sure for WinForms GUIs. Caveat: I don't actually know what Mono does with this on Linux. – Jon Skeet Nov 17 '09 at 20:27
10

since using \n is easier on the eyes (especailly when formatting), and also sometimes you don't control how the source string is constructed - I find best practice is to use:
TextBox1.Text = str.Replace("\r\n", "\n").Replace("\n", Environment.NewLine);

dancer42
  • 179
  • 1
  • 3
  • s = s.Replace("\r", "").Replace("\n", Environment.NewLine); – Sasha Bond Apr 20 '20 at 20:13
  • @SashaBond Much better IMO. Eliminates the edge case of a \r being present on its own whilst also neatly handling \n – Zintom Jul 08 '21 at 21:45
  • that's one old thread... anyway - \n and \r\n exist and mix everywhere... if there is just \r for some reason - it might be deliberate as no system I know of uses just carriage return to represent a newline. I would leave isolated '\r's in their places unless I have context and know they might exist and that they do represent a newline. – dancer42 Jul 10 '21 at 00:56
6

Usually \r\n gets me a newline in a textbox. Try replacing your \n with \r\n just be careful you don't have a mix of \r\n and \n

Bob
  • 97,670
  • 29
  • 122
  • 130
  • Good point! just be sure do textBox1.Text = generatedCode.Replace("\r\n", "\n"); to get any existing then do the textBox1.Text = generatedCode.Replace("\n", "\r\n"); Winform still rocks! – Rocklands.Cave Dec 02 '16 at 09:18
4

Add a carriage return (\r) and it should work:

TextBox1.Text = "First line\r\nSecond line";
MakoCSH
  • 71
  • 3