I have got an interesting dilemma where my application can run as a Console App or a Windows Forms App.
Since I do not want to write a shed load of code like this all over my application:
If ( IsConsoleApp() )
{
// process Console input and output
}
else
{
// process Windows input and output
}
To prevent this, I have decided to create two methods where I can pass in a TextReader and TextWriter instance and subsequently use these to process input and output, e.g.
public void SetOutputStream( TextWriter outputStream )
{
_outputStream = outputStream;
}
public void SetInputStream( TextReader inputStream )
{
_inputStream = inputStream;
}
// To use in a Console App:
SetOutputStream( Console.Out );
SetInputStream( Console.In );
To display some text in the Console window I just need to do something like this:
_outputStream.WriteLine( "Hello, World!");
And the text is magically redirected to the Console.
Now, my issue is how do I do something similar for a Windows application? I have created a form with a read-only Text Box control on it and I want the contents of the _outputStream
to be redirected to this text box in real-time.
Also, I want the _inputStream
to contain the contents of another Text Box control so that my App can read from this stream instead of the Text Box directly.
Thanks in advance.