C# - TextBox AppendText
C# - TextBox AppendText
Problem
This article demonstrates some reusable code that you can use to solve a problem related to newlines and TextBox control in its multiline mode. It can help really enhance the appearance and exactness of your Windows Forms program written in C#. The code is below and free for anyone to take and use.
Problem
In .NET, you are able to append text to a multiline TextBox with AppendText. This is great and it makes it easy to keep a log going of what's happening or events or turns or whatever. However, that will not append a newline or line feed to the end of the text, so when you call textBox1.AppendText again, the text will be on the same line.
///
/// Just a test method that shows the code being used.
///
private void Test()
{
for (int i = 0; i <>
{
textBox1.AppendText("Some text");
}
}
private void Test()
{
for (int i = 0; i <>
{
textBox1.AppendText("Some text");
}
}
Solution
Let's explore some alternatives, and then I will show you the very best way that I have found, that solves the entire newline problem with TextBox controls in Windows Forms. First, let me show you what happens when you always append a newline to the TextBox. My first reaction was to change the code to this:
///
/// Another test method.
///
private void Test()
{
for (int i = 0; i <>
{
textBox1.AppendText("Some text" + Environment.NewLine);
}
}
private void Test()
{
for (int i = 0; i <>
{
textBox1.AppendText("Some text" + Environment.NewLine);
}
}
That works fine, but it isn't as elegant in the code and it also has a small problem:
when the TextBox is appended to, there will be a blank line at the end. For
a scrolling log-style scenario, I don't want that blank line. This is what the two
calls would produce:
when the TextBox is appended to, there will be a blank line at the end. For
a scrolling log-style scenario, I don't want that blank line. This is what the two
calls would produce:
In one of my programs, that blank line caused a bit of a blemish on my UI. So I tried the following code. What I show next is the code and then what it will look like in your TextBox. As I show, it will put a blank line at the very start, which is very undesirable.
/// /// Another test.
///
private void Test()
{
for (int i = 0; i < style="font-weight: bold;">Best Solution
Nevermind--I whipped up a better solution and put it in the Windows form. The following code uses some conditional logic to test whether we need a newline on the TextBox append. This is better than AppendLine or any version of AppendText. What I show next is the new function, how you can call it, and then its output on the TextBox.
/// Append a line to the TextBox, and make sure the first and last
/// appends don't show extra space.
The string you want to show in the TextBox.
private void AppendTextBoxLine(string myStr)
{
if (textBox1.Text.Length > 0)
{
textBox1.AppendText(Environment.NewLine);
}
textBox1.AppendText(myStr);
}
private void AppendTextBoxLine(string myStr)
{
if (textBox1.Text.Length > 0)
{
textBox1.AppendText(Environment.NewLine);
}
textBox1.AppendText(myStr);
}
///
/// Just a test method that shows the above code being used.
///
private void TestMethod()
{
for (int i = 0; i <>
/// Just a test method that shows the above code being used.
///
private void TestMethod()
{
for (int i = 0; i <>
Labels: Multiline Textbox in C#.net
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home