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

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:

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

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

CODE:
  1. textBox1.Text = "Line One";
  2. textBox1.Text += Environment.NewLine;
  3. 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

If you have a low-traffic website, consider joining the Money4Banners advertising network. They will pay you £10 + £5 each month for displaying a small advert on three of your pages, regardless of traffic. American webmasters are welcome, and since £10 == $20, you make more!


9 Responses to “How To Add A New Line in a C# or Visual Basic TextBox”  

  1. Gravatar Icon 1 Namadev

    Hey,

    Thats a good one …

  2. Gravatar Icon 2 Dan

    Excellent - just what I was looking for! Thanks

  3. Gravatar Icon 3 OttoDisc

    Fast and clean.
    Many thanks!

  4. Gravatar Icon 4 Andrew

    Thx!! I really needed this! great ;)

  5. Gravatar Icon 5 praKash

    Thata was simple but hard to find :-)

  6. Gravatar Icon 6 marcexx66

    Thnxs , just what I need!

  7. Gravatar Icon 7 arasu

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

  8. Gravatar Icon 8 lmnd

    Yeahh. But not working un vb 6 :-(

  9. Gravatar Icon 9 JDuarte

    easier even is just vbNewLine in Visual Basic

    textBox1.Text=”First Line” vbNewLine “Second Line”

Leave a Reply

You must log in to post a comment.


Categories