by

How To Add A New Line in a C# or Visual Basic TextBox

June 11, 2011 in C#

If you're building a Windows Form Application, for example with C# or Visual Basic, then you may need to add a new line character, for example in a multiline TextBox. You do this using Enviroment.NewLine to insert the line break as follows:

textBox1.Text="First Line" + Environment.NewLine + "Second Line";

My preferred use is with the += operator in C#:

textBox1.Text = "Line One";
textBox1.Text += Environment.NewLine;
textBox1.Text += "Line Two";

This way, you can add extra lines of output to the TextBox as you require it.

References: Environment.NewLine on MSDN

10 responses to How To Add A New Line in a C# or Visual Basic TextBox

  1. Hey,

    Thats a good one ...

  2. Excellent - just what I was looking for! Thanks

  3. Fast and clean.
    Many thanks!

  4. Thx!! I really needed this! great ;)

  5. Thata was simple but hard to find :-)

  6. Thnxs , just what I need!

  7. Really it is excellent.Bez i was thinking for more hours but easily i found this!

  8. lmnd said on July 9, 2007

    Yeahh. But not working un vb 6 :-(

  9. easier even is just vbNewLine in Visual Basic

    textBox1.Text="First Line" vbNewLine "Second Line"

  10. It's working in VB 9.0
    Very Thanks to Webmasters.

    But about JDuarte solving, the correct code in VB 9.0 is:
    TextBox1.Text = "First Line" vbNewLine "Second Line"
    however it's good and easier.

Leave a reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>