Something like this:
string lineToAppend =
tb.Text = string.Format("{0}{1}{2}", tb.Text, System.Environment.NewLine, lineToAppend);
This is simple, but performance can be a problem if you do it repeatedly and and collect a lot of text (see below). Try this for better performance:
tb.SelectionStart = tb.Text.Length;
tb.SelectionLength = 0;
tb.SelectedText = lineToAppend;
Unfortunately, text box types do not support access by line directly; and appending performance is not good (because every time you append the whole
Text
it is copied back and forth again and again). For equivalent of such functionality, use
ListBox
and add or modify items of
string
type.
ListBox myListBox = new ListBox();
myListBox.Items.Add(lineToAppend);
ListBox
is much better then
TextBox
if you want appending some output results programmatically.
—SA