How To Use \n In a TextBox

后端 未结 4 1091
再見小時候
再見小時候 2021-01-01 11:34

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(

4条回答
  •  粉色の甜心
    2021-01-01 12:12

    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...

提交回复
热议问题