Redirecting Console.Write












2















I wrote the below-mentioned code from an amalgamation of c# tutorials about redirecting Console.Write or Console.WriteLine to a textbox text field. I realized this was bad since if I invoke any kind of .Close() method will erase the textbox which is the opposite of what I want to do.



Imports System.Text
Imports System.IO

Namespace ConsoleRedirection
Public Class TextBoxStreamWriter
Inherits TextWriter

Private _output As TextBox = Nothing

Public Sub New(ByVal output As TextBox)
_output = output
End Sub

Public Overrides Sub WriteLine(ByVal value As String)
MyBase.WriteLine(value)
_output.AppendText(String.Format("[{0}] {1}" + vbNewLine, DateTime.Now, value.ToString()))
End Sub

Public Overrides ReadOnly Property Encoding As Encoding
Get
Return System.Text.Encoding.UTF8
End Get
End Property
End Class
End Namespace


Is there anyway, besides loading a text file, that I can have all Console output redirected to either a textbox?





To answer a question from comments. (This code was originally found here: https://saezndaree.wordpress.com/2009/03/29/how-to-redirect-the-consoles-output-to-a-textbox-in-c/... But was converted over to VB)



In a separate form, the code is invoked by declaring a TextWriter.



Private _writer As TextWriter = New TextBoxStreamWriter(frmDebugLog.txtDebugLog)



Then as part of the .Load of a parent form:



Console.SetOut(_writer)


On a later portion of the form, the frmDebugLog is invoked by using frmDebugLog.Show()



When that window, frmDebugLog, is closed... that form and its textbox contents are discarded.










share|improve this question

























  • So, the title mentions a DataTable while the question mentions a TextBox or a DataGridView. You might want to proof-read your questions before submitting. Regardless, you can put whatever you want in that overridden WriteLine method. If you want to write to a DataTable then write to a DataTable. If you want to write to a DataGridView then do so. Just put whatever code you want executed in that WriteLine method. If you want to be able to write to different destinations then you could have multiple constructors.

    – jmcilhinney
    Nov 19 '18 at 23:13











  • @jmcilhinney I thought I edited that out, noted. Another idea I had, previously, was to redirect it to a datatable. Problem was, it's the same problem... I close the object, the table gets wiped. (That and I got lost on how to set up the table in the first place.) So I focused on the textbox still.

    – Paul Williams
    Nov 19 '18 at 23:22








  • 1





    OT: TextBoxStreamWriter is a bad name for that class because there is no Stream involved. The StreamWriter class is named that because it writes to a Stream. That class should just be named TextBoxWriter.

    – jmcilhinney
    Nov 19 '18 at 23:42











  • How exactly are you using that class? There's nothing about it that I can see that should cause a destination TextBox to be cleared as far as I can see. Maybe I'm missing something but I'd like to test it exactly as you're using it to see for myself.

    – jmcilhinney
    Nov 19 '18 at 23:45











  • @jmcilhinney Edited the additional details.

    – Paul Williams
    Nov 19 '18 at 23:59
















2















I wrote the below-mentioned code from an amalgamation of c# tutorials about redirecting Console.Write or Console.WriteLine to a textbox text field. I realized this was bad since if I invoke any kind of .Close() method will erase the textbox which is the opposite of what I want to do.



Imports System.Text
Imports System.IO

Namespace ConsoleRedirection
Public Class TextBoxStreamWriter
Inherits TextWriter

Private _output As TextBox = Nothing

Public Sub New(ByVal output As TextBox)
_output = output
End Sub

Public Overrides Sub WriteLine(ByVal value As String)
MyBase.WriteLine(value)
_output.AppendText(String.Format("[{0}] {1}" + vbNewLine, DateTime.Now, value.ToString()))
End Sub

Public Overrides ReadOnly Property Encoding As Encoding
Get
Return System.Text.Encoding.UTF8
End Get
End Property
End Class
End Namespace


Is there anyway, besides loading a text file, that I can have all Console output redirected to either a textbox?





To answer a question from comments. (This code was originally found here: https://saezndaree.wordpress.com/2009/03/29/how-to-redirect-the-consoles-output-to-a-textbox-in-c/... But was converted over to VB)



In a separate form, the code is invoked by declaring a TextWriter.



Private _writer As TextWriter = New TextBoxStreamWriter(frmDebugLog.txtDebugLog)



Then as part of the .Load of a parent form:



Console.SetOut(_writer)


On a later portion of the form, the frmDebugLog is invoked by using frmDebugLog.Show()



When that window, frmDebugLog, is closed... that form and its textbox contents are discarded.










share|improve this question

























  • So, the title mentions a DataTable while the question mentions a TextBox or a DataGridView. You might want to proof-read your questions before submitting. Regardless, you can put whatever you want in that overridden WriteLine method. If you want to write to a DataTable then write to a DataTable. If you want to write to a DataGridView then do so. Just put whatever code you want executed in that WriteLine method. If you want to be able to write to different destinations then you could have multiple constructors.

    – jmcilhinney
    Nov 19 '18 at 23:13











  • @jmcilhinney I thought I edited that out, noted. Another idea I had, previously, was to redirect it to a datatable. Problem was, it's the same problem... I close the object, the table gets wiped. (That and I got lost on how to set up the table in the first place.) So I focused on the textbox still.

    – Paul Williams
    Nov 19 '18 at 23:22








  • 1





    OT: TextBoxStreamWriter is a bad name for that class because there is no Stream involved. The StreamWriter class is named that because it writes to a Stream. That class should just be named TextBoxWriter.

    – jmcilhinney
    Nov 19 '18 at 23:42











  • How exactly are you using that class? There's nothing about it that I can see that should cause a destination TextBox to be cleared as far as I can see. Maybe I'm missing something but I'd like to test it exactly as you're using it to see for myself.

    – jmcilhinney
    Nov 19 '18 at 23:45











  • @jmcilhinney Edited the additional details.

    – Paul Williams
    Nov 19 '18 at 23:59














2












2








2








I wrote the below-mentioned code from an amalgamation of c# tutorials about redirecting Console.Write or Console.WriteLine to a textbox text field. I realized this was bad since if I invoke any kind of .Close() method will erase the textbox which is the opposite of what I want to do.



Imports System.Text
Imports System.IO

Namespace ConsoleRedirection
Public Class TextBoxStreamWriter
Inherits TextWriter

Private _output As TextBox = Nothing

Public Sub New(ByVal output As TextBox)
_output = output
End Sub

Public Overrides Sub WriteLine(ByVal value As String)
MyBase.WriteLine(value)
_output.AppendText(String.Format("[{0}] {1}" + vbNewLine, DateTime.Now, value.ToString()))
End Sub

Public Overrides ReadOnly Property Encoding As Encoding
Get
Return System.Text.Encoding.UTF8
End Get
End Property
End Class
End Namespace


Is there anyway, besides loading a text file, that I can have all Console output redirected to either a textbox?





To answer a question from comments. (This code was originally found here: https://saezndaree.wordpress.com/2009/03/29/how-to-redirect-the-consoles-output-to-a-textbox-in-c/... But was converted over to VB)



In a separate form, the code is invoked by declaring a TextWriter.



Private _writer As TextWriter = New TextBoxStreamWriter(frmDebugLog.txtDebugLog)



Then as part of the .Load of a parent form:



Console.SetOut(_writer)


On a later portion of the form, the frmDebugLog is invoked by using frmDebugLog.Show()



When that window, frmDebugLog, is closed... that form and its textbox contents are discarded.










share|improve this question
















I wrote the below-mentioned code from an amalgamation of c# tutorials about redirecting Console.Write or Console.WriteLine to a textbox text field. I realized this was bad since if I invoke any kind of .Close() method will erase the textbox which is the opposite of what I want to do.



Imports System.Text
Imports System.IO

Namespace ConsoleRedirection
Public Class TextBoxStreamWriter
Inherits TextWriter

Private _output As TextBox = Nothing

Public Sub New(ByVal output As TextBox)
_output = output
End Sub

Public Overrides Sub WriteLine(ByVal value As String)
MyBase.WriteLine(value)
_output.AppendText(String.Format("[{0}] {1}" + vbNewLine, DateTime.Now, value.ToString()))
End Sub

Public Overrides ReadOnly Property Encoding As Encoding
Get
Return System.Text.Encoding.UTF8
End Get
End Property
End Class
End Namespace


Is there anyway, besides loading a text file, that I can have all Console output redirected to either a textbox?





To answer a question from comments. (This code was originally found here: https://saezndaree.wordpress.com/2009/03/29/how-to-redirect-the-consoles-output-to-a-textbox-in-c/... But was converted over to VB)



In a separate form, the code is invoked by declaring a TextWriter.



Private _writer As TextWriter = New TextBoxStreamWriter(frmDebugLog.txtDebugLog)



Then as part of the .Load of a parent form:



Console.SetOut(_writer)


On a later portion of the form, the frmDebugLog is invoked by using frmDebugLog.Show()



When that window, frmDebugLog, is closed... that form and its textbox contents are discarded.







vb.net redirect console






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 23:57







Paul Williams

















asked Nov 19 '18 at 22:56









Paul WilliamsPaul Williams

83662856




83662856













  • So, the title mentions a DataTable while the question mentions a TextBox or a DataGridView. You might want to proof-read your questions before submitting. Regardless, you can put whatever you want in that overridden WriteLine method. If you want to write to a DataTable then write to a DataTable. If you want to write to a DataGridView then do so. Just put whatever code you want executed in that WriteLine method. If you want to be able to write to different destinations then you could have multiple constructors.

    – jmcilhinney
    Nov 19 '18 at 23:13











  • @jmcilhinney I thought I edited that out, noted. Another idea I had, previously, was to redirect it to a datatable. Problem was, it's the same problem... I close the object, the table gets wiped. (That and I got lost on how to set up the table in the first place.) So I focused on the textbox still.

    – Paul Williams
    Nov 19 '18 at 23:22








  • 1





    OT: TextBoxStreamWriter is a bad name for that class because there is no Stream involved. The StreamWriter class is named that because it writes to a Stream. That class should just be named TextBoxWriter.

    – jmcilhinney
    Nov 19 '18 at 23:42











  • How exactly are you using that class? There's nothing about it that I can see that should cause a destination TextBox to be cleared as far as I can see. Maybe I'm missing something but I'd like to test it exactly as you're using it to see for myself.

    – jmcilhinney
    Nov 19 '18 at 23:45











  • @jmcilhinney Edited the additional details.

    – Paul Williams
    Nov 19 '18 at 23:59



















  • So, the title mentions a DataTable while the question mentions a TextBox or a DataGridView. You might want to proof-read your questions before submitting. Regardless, you can put whatever you want in that overridden WriteLine method. If you want to write to a DataTable then write to a DataTable. If you want to write to a DataGridView then do so. Just put whatever code you want executed in that WriteLine method. If you want to be able to write to different destinations then you could have multiple constructors.

    – jmcilhinney
    Nov 19 '18 at 23:13











  • @jmcilhinney I thought I edited that out, noted. Another idea I had, previously, was to redirect it to a datatable. Problem was, it's the same problem... I close the object, the table gets wiped. (That and I got lost on how to set up the table in the first place.) So I focused on the textbox still.

    – Paul Williams
    Nov 19 '18 at 23:22








  • 1





    OT: TextBoxStreamWriter is a bad name for that class because there is no Stream involved. The StreamWriter class is named that because it writes to a Stream. That class should just be named TextBoxWriter.

    – jmcilhinney
    Nov 19 '18 at 23:42











  • How exactly are you using that class? There's nothing about it that I can see that should cause a destination TextBox to be cleared as far as I can see. Maybe I'm missing something but I'd like to test it exactly as you're using it to see for myself.

    – jmcilhinney
    Nov 19 '18 at 23:45











  • @jmcilhinney Edited the additional details.

    – Paul Williams
    Nov 19 '18 at 23:59

















So, the title mentions a DataTable while the question mentions a TextBox or a DataGridView. You might want to proof-read your questions before submitting. Regardless, you can put whatever you want in that overridden WriteLine method. If you want to write to a DataTable then write to a DataTable. If you want to write to a DataGridView then do so. Just put whatever code you want executed in that WriteLine method. If you want to be able to write to different destinations then you could have multiple constructors.

– jmcilhinney
Nov 19 '18 at 23:13





So, the title mentions a DataTable while the question mentions a TextBox or a DataGridView. You might want to proof-read your questions before submitting. Regardless, you can put whatever you want in that overridden WriteLine method. If you want to write to a DataTable then write to a DataTable. If you want to write to a DataGridView then do so. Just put whatever code you want executed in that WriteLine method. If you want to be able to write to different destinations then you could have multiple constructors.

– jmcilhinney
Nov 19 '18 at 23:13













@jmcilhinney I thought I edited that out, noted. Another idea I had, previously, was to redirect it to a datatable. Problem was, it's the same problem... I close the object, the table gets wiped. (That and I got lost on how to set up the table in the first place.) So I focused on the textbox still.

– Paul Williams
Nov 19 '18 at 23:22







@jmcilhinney I thought I edited that out, noted. Another idea I had, previously, was to redirect it to a datatable. Problem was, it's the same problem... I close the object, the table gets wiped. (That and I got lost on how to set up the table in the first place.) So I focused on the textbox still.

– Paul Williams
Nov 19 '18 at 23:22






1




1





OT: TextBoxStreamWriter is a bad name for that class because there is no Stream involved. The StreamWriter class is named that because it writes to a Stream. That class should just be named TextBoxWriter.

– jmcilhinney
Nov 19 '18 at 23:42





OT: TextBoxStreamWriter is a bad name for that class because there is no Stream involved. The StreamWriter class is named that because it writes to a Stream. That class should just be named TextBoxWriter.

– jmcilhinney
Nov 19 '18 at 23:42













How exactly are you using that class? There's nothing about it that I can see that should cause a destination TextBox to be cleared as far as I can see. Maybe I'm missing something but I'd like to test it exactly as you're using it to see for myself.

– jmcilhinney
Nov 19 '18 at 23:45





How exactly are you using that class? There's nothing about it that I can see that should cause a destination TextBox to be cleared as far as I can see. Maybe I'm missing something but I'd like to test it exactly as you're using it to see for myself.

– jmcilhinney
Nov 19 '18 at 23:45













@jmcilhinney Edited the additional details.

– Paul Williams
Nov 19 '18 at 23:59





@jmcilhinney Edited the additional details.

– Paul Williams
Nov 19 '18 at 23:59












1 Answer
1






active

oldest

votes


















2














It appears that you're using the default instance and so only ever one instance at a time. In that case, this will do the job:



Private Shared txtDebugLogText As String

Public Sub New()
' This call is required by the designer.
InitializeComponent()

' Add any initialization after the InitializeComponent() call.

txtDebugLog.Text = txtDebugLogText
End Sub

Protected Overrides Sub OnFormClosed(e As FormClosedEventArgs)
txtDebugLogText = txtDebugLog.Text
MyBase.OnFormClosed(e)
End Sub


By using a Shared field, you keep everything within the one class. The current value of that field is loaded into the TextBox first whenever a new instance is created and the text in the current TextBox is persisted to that field whenever an instance is destroyed.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383826%2fredirecting-console-write%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    It appears that you're using the default instance and so only ever one instance at a time. In that case, this will do the job:



    Private Shared txtDebugLogText As String

    Public Sub New()
    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.

    txtDebugLog.Text = txtDebugLogText
    End Sub

    Protected Overrides Sub OnFormClosed(e As FormClosedEventArgs)
    txtDebugLogText = txtDebugLog.Text
    MyBase.OnFormClosed(e)
    End Sub


    By using a Shared field, you keep everything within the one class. The current value of that field is loaded into the TextBox first whenever a new instance is created and the text in the current TextBox is persisted to that field whenever an instance is destroyed.






    share|improve this answer




























      2














      It appears that you're using the default instance and so only ever one instance at a time. In that case, this will do the job:



      Private Shared txtDebugLogText As String

      Public Sub New()
      ' This call is required by the designer.
      InitializeComponent()

      ' Add any initialization after the InitializeComponent() call.

      txtDebugLog.Text = txtDebugLogText
      End Sub

      Protected Overrides Sub OnFormClosed(e As FormClosedEventArgs)
      txtDebugLogText = txtDebugLog.Text
      MyBase.OnFormClosed(e)
      End Sub


      By using a Shared field, you keep everything within the one class. The current value of that field is loaded into the TextBox first whenever a new instance is created and the text in the current TextBox is persisted to that field whenever an instance is destroyed.






      share|improve this answer


























        2












        2








        2







        It appears that you're using the default instance and so only ever one instance at a time. In that case, this will do the job:



        Private Shared txtDebugLogText As String

        Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        txtDebugLog.Text = txtDebugLogText
        End Sub

        Protected Overrides Sub OnFormClosed(e As FormClosedEventArgs)
        txtDebugLogText = txtDebugLog.Text
        MyBase.OnFormClosed(e)
        End Sub


        By using a Shared field, you keep everything within the one class. The current value of that field is loaded into the TextBox first whenever a new instance is created and the text in the current TextBox is persisted to that field whenever an instance is destroyed.






        share|improve this answer













        It appears that you're using the default instance and so only ever one instance at a time. In that case, this will do the job:



        Private Shared txtDebugLogText As String

        Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        txtDebugLog.Text = txtDebugLogText
        End Sub

        Protected Overrides Sub OnFormClosed(e As FormClosedEventArgs)
        txtDebugLogText = txtDebugLog.Text
        MyBase.OnFormClosed(e)
        End Sub


        By using a Shared field, you keep everything within the one class. The current value of that field is loaded into the TextBox first whenever a new instance is created and the text in the current TextBox is persisted to that field whenever an instance is destroyed.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 1:14









        jmcilhinneyjmcilhinney

        25.9k22032




        25.9k22032
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383826%2fredirecting-console-write%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini