1

Ok so, let's say I am executing a script with IronPython in C#:

ScriptEngine engine = Python.CreateEngine();
string Script = textBox1.Text();
engine.Execute(Script);

Now let's say the script was:

print("Hello")

Output in VS Console: Hello What I am trying to achieve here is to redirect that output to a TextBox in C#. I have tried the following method which didn't work:

class TextBoxWriter : TextWriter
{
    private RichTextBox _textBox;

    public TextBoxWriter(RichTextBox textbox)
    {
        _textBox = textbox;
    }

    public override void Write(char value)
    {
        base.Write(value);
        _textBox.AppendText(value.ToString());
    }

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

Form1_Load:

engine.Runtime.IO.RedirectToConsole();
TextWriter _writer = TextWriter.Synchronized(new TextBoxWriter(textBox1));
Console.SetOut(_writer);

1 Answer 1

1

I have just that in my code and its functional for me.

engine.Runtime.IO.RedirectToConsole();    
Console.SetOut(new TextBoxWriter(textBox1)); 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.