I'm trying to forward the Console
output to a Windows Forms TextBox
control. So I attached a custom TextWriter
to the Console
which appends the output to the TextBox
.
But I think the TextWriter
or TextBox
is inaccessible from within an external class. How to fix this? Check my code below:
partial class Form1 : Form
{
public StringWriter _TextWriter;
public Form1()
{
InitializeComponent();
this._TextWriter = new TextBoxStreamWriter(this.textBox1);
Console.SetOut(this._TextWriter);
Console.WriteLine("This text does appear in the TextBox, works perfect.");
Test ConsoleOutputExternalClass = new Test();
}
}
public class TextBoxStreamWriter : StringWriter
{
TextBox _output = null;
public TextBoxStreamWriter(TextBox output)
{
this._output = output;
}
public override void WriteLine(string value)
{
base.WriteLine(value);
this._output.AppendText(value.ToString());
}
public override Encoding Encoding
{
get
{
return Encoding.UTF8;
}
}
}
private class Test
{
public Test()
{
// HERE I GET AN EXCEPTION ERROR !!
Console.WriteLine("System.IO.IOException: 'The handle is invalid.'");
}
}