Difference Between Label.Text And TextBox.Text (WinForms) In Treating Line Breaks

I had a small project recently that outputted the results of some calculations to the label on the form, I replaced the label with a textbox and noticed that instead of the line breaks I had in the label I got a bunch of non-printed characters. I had the following code that worked fine for the label:

myLabel.Text += "\nNew Line";

However as I said it didn’t work properly for the textbox, however this one did work:

myTextBox.Text += "\r\nNew line";

This is good and it also works with the Label control, however you can do it better by using Environment.NewLine which represents the new line string on the current system:

myTextBox.Text += Environment.NewLine + "New line";
Mike Borozdin (Twitter)
14 June 2008

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way. My personal thoughts tend to change, hence the articles in this blog might not provide an accurate reflection of my present standpoint.

© Mike Borozdin