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:
- Clone
- CopyTo
- Equals
- GetType
- GetHashCode
- GetEnumerator
- Initialize
- GetLowerBound
- GetUpperBound
- GetLength
- GetLongLength
- GetValue
- SetValue
- ToString
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 aList<string>
. There is simply no method available for the purpose. You must instead create a newstring[]
based on the current Lines reference, and assign it to Lines.Using a little Linq (.NET 3.5 or later):
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:
Even then, because the Lines array is a calculated property, this involves a lot of unnecessary conversion behind the scenes.
Just put a line break into your text.
You don't add lines as a method. Multiline just supports the use of line breaks.
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 appendingEnvironment.NewLine
or\r\n
to the.Text
, but also make sure your box is set up properly with the right properties.You have to use the
AppendText
method of the textbox directly. If you try to use theText
property, the textbox will not scroll down as new line are appended.Append a
\r\n
to the string to put the text on a new line.This will produce the two entries on separate lines.
Try this
you can also try
Where
\r
is carriage return and\n
is new line