C# TextBox.AppendText Method

You want to append lines of text to a TextBox control in your Windows Forms program with AppendText. You have to deal with newlines and avoid any extra line breaks, with the TextBox in the multiline mode.

TextBox in Windows Forms example

This C# article shows ways of using AppendText on TextBox controls. It requires Windows Forms.

Example

We will use AppendText on the TextBox. As a refresher, we can append text to a TextBox with the method AppendText. However, that will not append a newline or line feed to the end of the text, so when you call textBox1.AppendText("text") two times, the text will be on the same line.

Program that calls AppendText [C#]

private void Test()
{
    for (int i = 0; i < 2; i++)
    {
	textBox1.AppendText("Some text");
    }
}

Contents of TextBox

Some textSome text

Append lines

You can append lines by using Environment.NewLine and then adding some conditional logic to determine the first and last lines. This is better than any version of AppendLine or AppendText, in that it doesn't add extra newlines.

Program that calls AppendText [C#]

public partial class Form1 : Form
{
    private void AppendTextBoxLine(string myStr)
    {
	if (textBox1.Text.Length > 0)
	{
	    textBox1.AppendText(Environment.NewLine);
	}
	textBox1.AppendText(myStr);
    }

    private void TestMethod()
    {
	for (int i = 0; i < 2; i++)
	{
	    AppendTextBoxLine("Some text");
	}
    }
}

Contents of TextBox

Some text
Some text

Alternative methods

If you don't check the length of the Text in the TextBox, you will have unwanted line breaks in your TextBox. However, for less important or debugging code, you may prefer the slightly simpler method. The following code always appends newlines.

Program that calls AppendText [C#]

public partial class Browser : Form
{
    private void Test()
    {
	// Will leave a blank line on the end.
	for (int i = 0; i < 2; i++)
	{
	    textBox1.AppendText("Some text" + Environment.NewLine);
	}

	// Will leave a blank line on the start.
	for (int i = 0; i < 2; i++)
	{
	    textBox1.AppendText(Environment.NewLine + "Some text");
	}
    }
}

The following two tables demonstrate what the TextBox contents will look like when there is a newline on the end always, and when there is a newline on the start. This problem can be avoided using the method with the conditional check shown in this article.

Table 1

[Some text
 Some text
	  ]

Table 2

[Some text
 Some text]

Append strings

String type

Another concept that is useful for when you want to add strings to your TextBox is appending the strings themselves. This site has more complete and usable information on appending strings.

String Append: Add Strings Together

Summary

.NET Framework information

Here we saw how you can append lines to your TextBox gracefully and easily with methods written in the C# programming language. These methods work well with events like TextChanged, which together can really enhance the usability of the form.

TextChanged Event Windows Forms
.NET