Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Writing a log using a textbox

Tags:

c#

textbox

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 ?

like image 633
George Avatar asked Aug 28 '09 14:08

George


2 Answers

textBox1.Text +=  "new Log Message" + Environment.NewLine;

make the texbox not disabled, but readonly and the scrollbar will appear

like image 69
Arsen Mkrtchyan Avatar answered Oct 11 '22 18:10

Arsen Mkrtchyan


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)

like image 36
sbecker Avatar answered Oct 11 '22 17:10

sbecker