I have wrote a simple control. But it does not work with colors (its derived from textWriter, which does not support colors).
internal class TextBoxWriter : TextWriter {
TextBox _output;
public TextBoxWriter(TextBox output) {
_output = output;
}
public override void WriteLine(string value) {
Write(value + System.Console.Out.NewLine);
}
public override void Write(string value) {
if(_output.InvokeRequired) {
_output.BeginInvoke((Action<string>)Write, value);
} else {
_output.AppendText(value);
}
}
public override void Write(char value) {
Write(value.ToString());
}
public override Encoding Encoding {
get { return Encoding.UTF8; }
}
}
Usage:
//designer code
private System.Windows.Forms.TextBox outputTextBox;
this.outputTextBox = new System.Windows.Forms.TextBox();
//user code
var _textWriter = new TextBoxWriter();
System.Console.SetOut(_textWriter);
Console.WriteLine("hello"); //this will be show in the outputTextBox