I think this would be easier:
Define the Console methods you use with exact same name and signatures:
internal class UiConsole {
internal UiConsole(TextBox sink) { this.Sink = sink; }
internal string void WriteLine(string value) { }
internal string void WriteLine() { }
internal string void WriteLine(string format, params object[] values) {
string stringValue = string.Format(format, values);
}
TextBox Sink;
Replace
Console
with you new class somehow, the way you would not need to change a single character in the part of code which writes your output. This is easy, too. Next time, spend some time to write output interface and supply, say, console implementation of it. When you need to direct output somewhere else, just change the implementation.
[EDIT]
For example, you can use it like this:
class SomeUseOfOutput {
internal SomeUseOfOutput(UiConsole console) { this.Console = console; }
void OutputExample() {
Console.WriteLine("Starting...");
Console.WriteLine("Iteration #{0}: x={1}, y={2}", count, x, y);
}
UiConsole Console;
}
In this example, the method
OutputExample
can work outside of this class if you use
using System
with
System.Console
; when you put it in the class
SomeUseOfOutput
, it starts working with its instance of
UiConsole Console
and write the same thing to your text box. In this way, there is no need to modify the code of
OutputExample
in any way.
By the way, I usually do exact same thing, but use
ListBox
, not
TextBox
. The
TextBox
does not have append operation, and appending becomes too expensive if you use
Text
property and the size of the text buffer becomes too long. I demonstrated high-performance append to
TextBox<code> in response to one of the questions; it never uses <code>Text
, but manipulates selection and text length, the append is done via assigning the text to be appended to
SelectedText
property. (In fact, I described the pseudo-code.) With
ListBox
, you only add a line and adjust scrolling (select new line).
[END EDIT]
By the way, want to ask you just in case: do you know that you can still use console in the WPF or Forms application (or anywhere)?
Just create a windowed application. Then change the application type to "Console Application" in the project properties. I won't disrupt your application, it will only show a console. "Console Application" simply means: "among other thing, show a console". "Window Application" simply means: hide the console. The names of those types and the fact of using combo box is really misleading. Those types are not alternative to each other as one would think.
—SA