Reputation: 407
I am quite new to C# (I had an RS-232 problem recently) and am trying to write an small application which does various tasks behind the scenes and I want to create a log of messages to keep the user updated on what stage the application is at.
The log will just consist of simple one line messages.
I am currently using a textbox that is disabled so that the user cannot change it. I can obviously do multiple text lines using the \r\n characters at the end of a line, but when I come to write a 2nd set of messages they are written at the begining of the text box overwriting the first messages.
Can I change this to append rather than overwrite ? Also, will the text box automatically add a scroll barr when the text is more than the box can display ?
Upvotes: 11
Views: 23874
Reputation: 584
A better option is to use:
TextBox.AppendText(string)
for example:
myTextBox.AppendText(newLogEntry + Environment.NewLine);
Which makes a multi line textbox scroll to the button as well as to avoid flickering of the scroll bar.
MSDN on TextBox.AppendText(string)
Upvotes: 6
Reputation: 50752
textBox1.Text += "new Log Message" + Environment.NewLine;
make the texbox not disabled, but readonly and the scrollbar will appear
Upvotes: 7
Reputation: 6295
You can append to the text using this piece of code:
myTextBox.Text += myLogText + Environment.NewLine;
Alternatively, you can use a ListBox.
Upvotes: 2
Reputation: 50235
To add it at the top:
textBox.Text = newText + Environment.NewLine + textBox.Text;
To append it:
textBox.Text += Environment.NewLine + newText;
Yes, a scroll bar is added automatically.
Upvotes: 4