Windows Develop Bookmark and Share   
 index > Windows Forms Sample Applications > Can I print to a RichTextBox using a StreamWriter?
 

Can I print to a RichTextBox using a StreamWriter?

Greetings,

I'm trying to write an app that can run in a console or as a windows form. I have a worker class that handles all the work, and the console (or the form) will act as I/O to the worker class.

My question is how to handle the output printing in the worker class. I was thinking of instantiating a StreamWriter in my main(), and passing the StreamWriter to my worker class. In main(), after I examine the args, I can tell if I'm going to run in a console or not. So I was going to set the streamwriter to the standout output stream if the app runs in a console.

I was wondering if it's possible to point the streamwriter to a RichTextBoxin my GUI form so that my worker class can just use the streamwriter for writing, with the actual output location being transparent to the worker.

Is this kind of this doable? If not, is there a better way to have my worker class point to the correct output?

Adam Volker-Yoblick  Wednesday, July 11, 2007 2:02 PM
Gavin Jin - MSFT  Friday, July 13, 2007 8:29 AM

That article talks about printing the contents of a RichTextBox to an output device. This isn't what I'm trying to do.

I'm trying to use a StreamWriter (or something similar) to write TO a RichTextBox's richTextBox.Text field. I do not want to use richTextBox.AppendText().

I'm trying to make the output transparent to my worker class. It will not know what it's printing to, just that it's sending output. My reason for this is I want my application to be able to run in a console window if arguments are passed in, or in a winform if there are no arguments.

In the case where there ARE arguments, the worker class will get its inputs from the command line and will send its outputs to the Console output.

In the case where there are NO arguments, the worker class will get its inputs from the WinForm, and will send its outputs to the Winform's RichTextBox.

I wanted to know if there's a type of object I can use for outputting to both the Console and a RichTextBox. This is why I was asking about writing to a RichTextBox with a StreamWriter. Is this possible? If not, is there another type of object that will allow both output to the Console and to a RichTextBox?

Thanks

Adam Volker-Yoblick  Friday, July 13, 2007 1:18 PM
Hey there. My solution was to create a concrete TextWriter class that I called "RichTextBoxTextWriter". See below:

=============================================================================

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace Technica.AppFuzz.IO
{

public class RichTextBoxTextWriter : TextWriter
{

#region Fields

private Int32 _bufferSize = 10000;
private List<RichTextBox> _rtbOutputs = new List<RichTextBox>( 1 );

#endregion

#region Public Property: Encoding

public override Encoding Encoding
{
get
{
return System.Text.Encoding.Default;
}
}

#endregion

#region Public Properties

public Int32 BufferSize
{
get
{

return this._bufferSize;
}
set
{
this._bufferSize = value;
}
}

#endregion

#region Public Method: AttachRtbOutput

public void AttachRtbOutput( RichTextBox rtbOutput )
{
this._rtbOutputs.Add( rtbOutput );
}

#endregion

#region Public Method: DetachRtbOutput

public void DetachRtbOutput( RichTextBox rtbOutput )
{
this._rtbOutputs.Remove( rtbOutput );
}

#endregion

#region Public Method: WriteLine, Write

public override void WriteLine( string format, params object[] arg )
{
this.Write( String.Format( format, arg ) + System.Environment.NewLine );
}

public override void WriteLine( string value )
{
this.Write( value + System.Environment.NewLine );
}

public override void Write( string format, params object[] arg )
{
this.Write( String.Format( format, arg ) );
}

public override void Write( string value )
{
foreach( RichTextBox rtbOutput in this._rtbOutputs )
{
if( rtbOutput.InvokeRequired )
{
rtbOutput.Invoke(
new WriteMessageToRtfEventHandler( WriteMessageToRtb ),
new Object[] { rtbOutput, value }
);
}
else
{
this.WriteMessageToRtb( rtbOutput, value );
}
}
}

private delegate void WriteMessageToRtfEventHandler( RichTextBox rtbOutput, String message );

private void WriteMessageToRtb( RichTextBox rtbOutput, String message )
{
if( rtbOutput.Text.Length > this._bufferSize )
rtbOutput.Text = String.Format( "{0}: {1}", System.DateTime.Now.ToString(), message );
else
rtbOutput.Text += String.Format( "{0}: {1}", System.DateTime.Now.ToString(), message );
rtbOutput.Focus();
rtbOutput.SelectionStart = rtbOutput.Text.Length;
}

#endregion

}

}

=============================================================================

note that I'm only implementing a few of the possible Write() and WriteLine() signatures.

You can use this class to, for example, connect a rich text box to Console.Err:

RichTextBoxTextWriter errorWriter = new RichTextBoxTextWriter();
errorWriter.AttachRtbOutput( this.rtbError );
Console.SetError( errorWriter );

where this.rtbError is rich text box control. From here, anything written to Console.Err will go to my this.rtbError.


hth

C Newby  Sunday, September 30, 2007 11:28 PM

You can use google to search for other answers

Custom Search

More Threads

• PocketVision - References problem
• Issue Vision served us well
• C# help
• Return Not displayed properly
• Host XSD Designer on Winform application
• I download a TaskVision Source File,but I connot find the web service source part or the db definition part
• Terrarium .net 2.0 Problem
• ??record video stream??
• DesignVision -- Formats
• Hi!