August 22, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Redirect I/O to a TextBoxWriter in .NET

  • April 17, 2006
  • By Paul Kimmel
  • Send Email »
  • More Articles »

Redirecting Console I/O

Listing 5 shows the complete code for the TextBoxWriter.

Listing 5: Complete Listing for TextBoxWriter

Imports System.Windows.Forms
Imports System.Text
Public Class TextBoxWriter
   Inherits System.IO.TextWriter
   Private control As TextBoxBase
   Private Builder As StringBuilder
   Public Sub New(ByVal control As TextBox)
      Me.control = control
      AddHandler control.HandleCreated, _
         New EventHandler(AddressOf OnHandleCreated)
   End Sub
   Public Overrides Sub Write(ByVal ch As Char)
      Write(ch.ToString())
   End Sub
   Public Overrides Sub Write(ByVal s As String)
      If (control.IsHandleCreated) Then
         AppendText(s)
      Else
         BufferText(s)
      End If
   End Sub
   Public Overrides Sub WriteLine(ByVal s As String)
      Write(s + Environment.NewLine)
   End Sub
   Private Sub BufferText(ByVal s As String)
      If (Builder Is Nothing) Then
         Builder = New StringBuilder()
      End If
      Builder.Append(s)
   End Sub
   Private Sub AppendText(ByVal s As String)
      If (Builder Is Nothing = False) Then
         control.AppendText(Builder.ToString())
         Builder = Nothing
      End If
      control.AppendText(s)
   End Sub
   Private Sub OnHandleCreated(ByVal sender As Object, _
      ByVal e As EventArgs)
      If (Builder Is Nothing = False) Then
         control.AppendText(Builder.ToString())
         Builder = Nothing
      End If
   End Sub
   Public Overrides ReadOnly Property Encoding() As System.Text.Encoding
      Get
         Return Encoding.Default
      End Get
   End Property
End Class

To use the TextBoxWriter, you need a TextBox control and you need to create an instance of the TextBoxWriter. You use the TextBox to initialize the TextBoxWriter, and you pass the TextBoxWriter to Console.SetOut to change the value of the Console.Out property. (See Listing 6 for the form that contains the TextBox and the code that redirects the Console output stream.)

Listing 6: The Form Containing the Textbox and the Redirect Code for the Console Output Stream

Public Class Form1
   Private writer As TextBoxWriter = Nothing
   Private Sub Form1_Load(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles MyBase.Load
      writer = New TextBoxWriter(TextBox1)
      Console.SetOut(writer)
   End Sub
   Private Sub Button1_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles Button1.Click
      Console.WriteLine("This is some redirected text")
   End Sub
End Class

The result of the code that redirects the Console output stream will look like Figure 1.

Figure 1: Output Sent to the Redirect Console

About the Author

Paul Kimmel is the VB Today columnist for www.codeguru.com and has written several books on object-oriented programming and .NET. Check out his new book UML DeMystified from McGraw-Hill/Osborne. Paul is an architect for Tri-State Hospital Supply Corporation. You may contact him for technology questions at pkimmel@softconcepts.com.

If you are interested in joining or sponsoring a .NET Users Group, check out www.glugnet.org.

Copyright © 2006 by Paul T. Kimmel. All Rights Reserved.





Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel