Getting Console.Out to Forms.Textbox

  • Thread starter Thread starter George W.
  • Start date Start date
G

George W.

I have several classes as part of a Console Application that sent output
to the Console. I want to use these classes as part of my Windows App
and I'm looking for a way to send the Console.Out to a TextBox or
something similar in my Form. I already know about Console.SetOut() but
I want the TextBox to update as text is sent to the standard output. I'm
wodnering if anyone has some tips.

Thanks in advance,
GW
 
Hi George,

Based on my understanding, you want to redirect your Console output to a
textbox on the form.

I think you may first redirect the Console output to StringWriter
class(Which is a child class of TextWriter), then, you may write the
StringWriter to the TextBox.

Do like this:

public class MyConsole
{
public void outputstring()
{
Console.WriteLine("Can you receive my output?");
}
}

private void button1_Click(object sender, System.EventArgs e)
{
MyConsole obj=new MyConsole();
System.IO.StringWriter sw=new System.IO.StringWriter();
Console.SetOut(sw);
obj.outputstring();
this.textBox1.Text=sw.ToString();
}

Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
This would work, but not for my purposes. I need the TextBox control to
dynamically display the standard output as text is written to it via
various Console.WriteLine calls. So basically, I want my TextBox control
to act as a Console window.
 
No. I'm trying to get a TextBox control in my form to act as a Console
Window for purposes of output.
 
George W. said:
This would work, but not for my purposes. I need the TextBox control to
dynamically display the standard output as text is written to it via
various Console.WriteLine calls. So basically, I want my TextBox control
to act as a Console window.

Derive from TextWriter yourself in a way that makes it write to the
TextBox every time it's written to, and then use Console.SetOut to set
the console output to your new class.
 
Hi George,

Thanks for your feedback.

I am still not fully understand your reqirement. Can you explain more clear
what behavior does "TextBox control to act as a Console window"? Please
show me some details description of you wanted behaviors.

For your "TextBox control to dynamically display the standard output as
text is written to it via various Console.WriteLine calls", what does
"dynamically disply standard output" mean? In my given sample, after
Console.SetOut(sw) is invoked, all the Console.WriteLine will "dynamically
display" in TextBox. It does not meet your need?

Please feel free to feedback.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi George,

Is your problem resolved? Do you still have any concern on this issue?

Please feel free to feedback, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Sorry,

Guess I should've been more clear. By Dynamically, i mean that I desire
any text written to Console.Out to appear in my form as it is written.
So, if a class in my application calls Console.WriteLine() then I want
that string to appear in my form somehow. If I
Console.SetOut(someStringWriter) and then use:
TextBox1.Text = someStringWriter.ToString()
I won't get a constantly updating stream of information. (At least not
by putting it on my Submit button.)

Hope this makes more sense now.

Thanks,
George W.
 
Hi George,

Thanks very much for your feedback.

I see your concern. To get a chance to be notified when Console.WriteLine
write string to StringWriter. You should override StringWriter class, then
override all the overloading versions StringWriter.Write method. In your
own StringWriter.Write method, after invoking the base class's
StringWriter.Write, then update the TextBox.Text prooperty.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top