How to add a line to a multiline TextBox?

I would go with the System.Environment. NewLine or a StringBuilder.

I would go with the System.Environment. NewLine or a StringBuilder Then you could add lines with a string builder like this: StringBuilder sb = new StringBuiler(); sb. AppendLine("brown"); sb.

AppendLine("brwn"); textbox1. Text += sb.ToString(); or NewLine like this: textbox1. Text += System.Environment.

NewLine + "brown.

Maybe it should have been sb. AppendLine("brown")... – Casperah Dec 16 at 16:23 yes good point! – Matt Dec 16 at 16:25.

Append a \n to the string. TextBox1. Text += ("brown\r\n"); textBox1.

Text += ("brwn"); This will produce the two entries on separate lines.

It won't work, TextBox control will not handle the \n properly, you must use \r\n for that, please check yourself – Vamsi Krishna Dec 16 at 16:35 Good call @VamsiKrishna – DJ Quimby Dec 16 at 17:43.

Just put a line break into your text. You don't add lines as a method. Multiline just supports the use of line breaks.

Try this textBox1. Text += "SomeText\r\n" you can also try textBox1. Text += "SomeText" + Environment.

NewLine; Where \r is carriage return and \n is new line.

You have to use the AppendText method of the textbox directly. If you try to use the Text property, the textbox will not scroll down as new line are appended. TextBox1.

AppendText("NewLine).

The "Lines" property of a TextBox is an array of strings. By definition, you cannot add elements to an existing string, like you can to a List. There is simply no method available for the purpose.

You must instead create a new string based on the current Lines reference, and assign it to Lines. Using a little Linq (.NET 3.5 or later): textBox1. Lines = textBox.Lines.

Concat(new{"Some Text"}).ToArray(); This code is fine for adding one new line at a time based on user interaction, but for initializing a textbox with a few dozen new lines, it will perform very poorly. If you're setting the initial value of a TextBox, I would either set the Text property directly using a StringBuilder (as other answers have mentioned), or if you're set on manipulating the Lines property, use a List to compile the collection of values and then convert it to an array to assign to Lines: var myLines = new List(); myLines. Add("brown"); myLines.

Add("brwn"); myLines. Add("brn"); myLines. Add("brow"); myLines.

Add("br"); myLines. Add("brw"); ... textBox1. Lines = myLines.ToArray(); Even then, because the Lines array is a calculated property, this involves a lot of unnecessary conversion behind the scenes.

Casperah pointed out that i'm thinking about it wrong. A TextBox doesn't have lines, it has text. That text can be split on the CRLF into lines, if requested - but there is no notion of lines.

The question then is how to accomplish what I want, rather than what WinForms lets me. Other given variants have a subtle bug: textBox1. AppendText("NewLine); textBox1.

AppendText("Text += "Text += System.Environment. NewLine + "brown"; They either append or prepend a newline when one (might) not be required. So, extension helper: public static class WinFormsExtensions { public static void AppendLine(this TextBox source, string value) { if (source.Text.

Length==0) source. Text = value; else source. AppendText("\r\n"+value); } } So now: textBox1.Clear(); textBox1.

AppendLine("red"); textBox1. AppendLine("green"); textBox1. AppendLine("blue"); and textBox1.

AppendLine(String. Format("Processing file {0}", filename)).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions