Reputation: 20644
If I have a GUI like this:
---------------------
[Button]
---------------------
[RichTextbox_output]
---------------------
Would it be possible to show all:
stdout and stderr in the richTextbox that I can see from 'output' diaglog of visual studio?
Thanks in advance.
Upvotes: 1
Views: 833
Reputation: 5744
Stdout in C# is called Console.Out
. Stderr is called Console.Error
.
To solve this you will need to redirect Console.Out
. This can be done using Console.SetOut
. This will then need to be redirected to a custom stringwriter of your own that will write to the rich text box. This isn't built into the textbox and you will need to create a stream implementation of your own. One possible solution is to subclass StringWriter
and overload the WriteLine
method to push to your textbox instead.
public class TextBoxWriter : StringWriter
{
public override void WriteLine(string x)
{
// Write to textbox here
}
}
Obviously, if you use more than just WriteLine
you'll have to create overloads for those methods as well.
The same approach can be taken with Console.SetError
for the stderr stream.
Upvotes: 5
Reputation: 552
For the stderr you'll have to encapsulate the program in a try/catch block. Where you catch all the errors. For the stdout there are two options, overwrite the stdout to write to the textbox or simply write to the textBox everywhere instead of writing to stdout.
For the exceptions:
try {
// your program
catch (Exception e){
RichTextbox_output.AppendText()
}
This example requires the RichTextbox be within scope though, so placement in the program might be tricky.
For every place where you use stdout, in place of Console.write, use RichTextbox_output.AppendText()
Upvotes: 0