Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Binding System.Console with a TextBox
 

Binding System.Console with a TextBox

I am using VS 2005 best and .Net 2.0

How can I bind the System.Console to the textbox control so everytime something is written to the console with Console.WriteLine() or Console.Write() it will be displayed in my text control?

meatago  Thursday, October 06, 2005 11:57 PM
Using the SetOut method of the System.Console class, you can redirect the standard output from a console to a different stream.  In theory, you could then handle this stream and redirect it to a textbox or any other control.

Here is sample code from henrikk in this forum from last year (I can't show a link since it was no longer available, pulled it from a google cache):



// An implementation of redirecting System.Console.WriteLine output
// from the standard TextWriter output stream to a new stream that
// can (for example) display the output in a Windows control.

using System;
using System.IO;
using System.Windows.Forms;

// The new class derived from StringWriter (of abstract type TextWriter)
// that implements a new WriteLine method to do whatever we want with what
// Console.WriteLine outputs. In this case we display the output in a
// RichTextBox control. A complete implementation may beed to override more
// methods than just "WriteLine".

public class StringRedir : StringWriter

   private RichTextBox outBox; 

   public StringRedir(ref RichTextBox textBox) 
   { 
      outBox = textBox; 
   } 

   public override void WriteLine(string x) 
   { 
      outBox.Text += x + "\n"; 
      outBox.Refresh(); 
   }
}

// Here is how we would use the above StringRedir class.
public class ConsoleRedir : Form

   private RichTextBox richTextBox; 

   public ConsoleRedir() 
   { 
      // Create the RichTextBox control that will hold our output. 
      this.richTextBox = new System.Windows.Forms.RichTextBox (); 
      this.richTextBox.Dock = System.Windows.Forms.DockStyle.Fill; 
      this.Controls.Add(this.richTextBox); 

      // Here we redirect Console.WriteLine to a RichTextBox control. 
      TextWriter tmp = Console.Out; // Save the current console TextWriter. 
      StringRedir r = new StringRedir(ref richTextBox); 
      Console.SetOut(r); // Set console output to the StringRedir class. 

      System.Console.WriteLine("Redirecting 'Console.WriteLine' to a RichTextBox."); 
      System.Console.WriteLine("Bye for now!"); 

      Console.SetOut(tmp); // Redirect Console back to original TextWriter. 
      r.Close(); // Close our StringRedir TextWriter. 
   } 

   static void Main() 
   { 
      Application.Run(new ConsoleRedir()); 
   }
}

 



However, I think there are probably better options.  Why not write directly to the textbox, instead of using Console.WriteLine()?  Or write to your own class that can then direct the output to wherever you need it (useful for status and error logging.

I didn't check the above code, so let me know if it doesn't work I can help,
Best of luck,
Josh Lindenmuth
Josh Lindenmuth  Friday, October 07, 2005 12:49 AM
This method does work.  I just implemented it in a couple minutes.  However, I'm not sure if it is the "ideal" solution.  I was struggling to use the Databinding properties menus and dialog boxes in visual studio to achieve the desired effect without writing any code. 

But again...this does work.

Thanks!
meatago  Friday, October 07, 2005 1:08 AM

You can use google to search for other answers

Custom Search

More Threads

• Use of Filter field for BindingSource
• DataGridView (Custom DataGridViewTextBoxColumn) Sorting Problem
• datagridview using tag
• Dynamic DataGridView height
• Problems Using Npgsql with DataGridView
• Report Viewer Control ?
• Insert new row using BindingSource
• guard against 2 users updating the same row
• Update database
• How to: Concerning Datagrid