How can i add a line of text to a multi-line TextBox
?
e.g. pseudocode;
textBox1.Clear();
textBox1.Lines.Add("1000+");
textBox1.Lines.Add("750-999");
textBox1.Lines.Add("400-749");
...snip...
textBox1.Lines.Add("40-59");
or
textBox1.Lines.Append("brown");
textBox1.Lines.Append("brwn");
textBox1.Lines.Append("brn");
textBox1.Lines.Append("brow");
textBox1.Lines.Append("br");
textBox1.Lines.Append("brw");
textBox1.Lines.Append("brwm");
textBox1.Lines.Append("bron");
textBox1.Lines.Append("bwn");
textBox1.Lines.Append("brnw");
textBox1.Lines.Append("bren");
textBox1.Lines.Append("broe");
textBox1.Lines.Append("bewn");
The only methods that TextBox.Lines implements (that i can see) are:
Above methods did not work for me. I got the following exception:
Exception : 'System.InvalidOperationException' in System.Windows.Forms.dll
Turns out I needed to call Invoke
on my controls first. See answer here.
If you know how many lines you want, create an array of String with that many members (e.g. myStringArray). Then use myListBox.Lines = myStringArray;
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
Just put a line break into your text.
You don't add lines as a method. Multiline just supports the use of line breaks.
Append a \r\n
to the string to put the text on a new line.
textBox1.Text += ("brown\r\n");
textBox1.Text += ("brwn");
This will produce the two entries on separate lines.
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<string>
. 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<string>();
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.
The adding of Environment.NewLine
or \r\n
was not working for me, initially, with my textbox. I found I had forgotten to go into the textbox's Behavior properties and set the "Multiline" property to "True" for it to add the lines! I just thought I'd add this caveat since no one else did in the answers, above, and I had thought the box was just going to auto-expand and forgot I needed to actually set the Mulitline property for it to work. I know it's sort of a bonehead thing (which is the kind of thing that happens to us late on a Friday afternoon), but it might help someone remember to check that. Also, in the Appearance section is the "ScrollBars" property that I needed to set to "Both", to get both horizontal and vertical bars so that text could actually be scrolled and seen in its entirety. So the answer here isn't just a code one by appending Environment.NewLine
or \r\n
to the .Text
, but also make sure your box is set up properly with the right properties.
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 StringBuilder();
sb.AppendLine("brown");
sb.AppendLine("brwn");
textbox1.Text += sb.ToString();
or NewLine like this:
textbox1.Text += System.Environment.NewLine + "brown";
Better:
StringBuilder sb = new StringBuilder(textbox1.Text);
sb.AppendLine("brown");
sb.AppendLine("brwn");
textbox1.Text = sb.ToString();
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("Hello" + Environment.NewLine);
Source: Stackoverflow.com