title: Redirecting Console Output to winforms ListBox
body: <p>I have built a library that dumps most of its debug text using Console.WriteLine(); </p>
  
  <p>I am now I am the process of using the library in a Windows Forms Application, and still need access to the Console output. ( To Display in a List/RichText box ) </p>
  
  <p>I Noticed I can override the standard out of the console to a TextWriter, but how would I then get this data into the display .  </p>
  
  <p>I Tried doing something along the lines of</p>
  
  <pre><code>  public partial class Form1 : Form
    {
      Timer T;
      MemoryStream mem;
      StreamWriter writer; 
  
  
      public Form1()
      {
  
          InitializeComponent();
          mem = new MemoryStream(1000);
          writer = new StreamWriter(mem);
          Console.SetOut(writer);
  
          T = new Timer();
          T.Interval = 250; // yes this probally is to short.
          T.Tick += new EventHandler(T_Tick);
          T.Start();
  
  
          Console.WriteLine("output");
          Console.WriteLine("AnotherLine");
      }
  
      void T_Tick(object sender, EventArgs e)
      {
          string s = Encoding.Default.GetString(mem.ToArray());
          string[] Lines = s.Split(Environment.NewLine.ToCharArray());
          Output.Items.Clear(); // Output is a listbox 
          foreach (string str in Lines)
              Output.Items.Add(str);
      }
  }
  </code></pre>
  
  <p>but to no avail. Any ideas? </p>
  
  <p>Unneeded code removed. </p>