将Console.WriteLine()重定向到文本框 [英] Redirecting Console.WriteLine() to Textbox

查看:95
本文介绍了将Console.WriteLine()重定向到文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#在Visual Studio 2010中构建此应用程序.

I'm building this application in Visual Studio 2010 using C#.

基本上有2个文件,即form1.cs(这是Windows窗体)和program.cs(所有逻辑所在).

Basically there are 2 files, form1.cs (which is the windows form) and program.cs (where all the logic lies).

//form1.cs
public partial class Form1 : Form
{
    //runButton_click function
}

//program.cs
class Program
{
    static void Main()
    {
        while(blah-condition)
        {
            //some calculation
            Console.WriteLine("Progress " + percent + "% completed.");
        }
    }
}

有一个运行按钮和一个空白的文本框.

There is a Run button and a blank textbox.

当用户单击运行" 按钮时,program.cs将执行某些任务,并使用 Console.WriteLine()不断将进度打印到控制台上(命令提示符)).

When the user hits the Run button, program.cs will perform some task and constantly printing out the progress using Console.WriteLine() onto the console (command prompt).

问题:如何在Form1上打印到文本框,而不是打印到命令提示符下?我将需要不断打印进度,而无需任何用户操作.

Question: How can I print to the textbox on form1 instead of printing into command prompt? I will need to print the progress constantly without any user action.

提前谢谢!

顺便说一句,它不必是文本框,也可以是标签或可以接受文本的其他东西.我选择文本框是因为它对我来说更有意义.

By the way, it doesn't have to be a textbox, it can be a label or something else that can take text. I chose textbox because it makes more sense to me.

推荐答案

首先创建一个能够写入文本框的新 TextWriter .只需需要覆盖接受 char Write 方法,但是这样做效率低下,因此最好至少覆盖该方法加上一个字符串.

Start by creating a new TextWriter that is capable of writing to a textbox. It only needs to override the Write method that accepts a char, but that would be ungodly inefficient, so it's better to overwrite at least the method with a string.

public class ControlWriter : TextWriter
{
    private Control textbox;
    public ControlWriter(Control textbox)
    {
        this.textbox = textbox;
    }

    public override void Write(char value)
    {
        textbox.Text += value;
    }

    public override void Write(string value)
    {
        textbox.Text += value;
    }

    public override Encoding Encoding
    {
        get { return Encoding.ASCII; }
    }
}

在这种情况下,我只接受了一个 Control ,它可以是一个 Textbox ,一个 Label 或其他任何东西.如果您想将其更改为 Label ,就可以了.

In this case I've had it just accept a Control, which could be a Textbox, a Label, or whatever. If you want to change it to just a Label that would be fine.

然后仅将控制台输出设置为该编写器的新实例,指向某些文本框或标签:

Then just set the console output to a new instance of this writer, pointing to some textbox or label:

Console.SetOut(new ControlWriter(textbox1));

如果您希望将输出写入控制台以及到文本框,我们可以使用此类创建一个写入器,该写入器将写入多个写入器:

If you want the output to be written to the console as well as to the textbox we can use this class to create a writer that will write to several writers:

public class MultiTextWriter : TextWriter
{
    private IEnumerable<TextWriter> writers;
    public MultiTextWriter(IEnumerable<TextWriter> writers)
    {
        this.writers = writers.ToList();
    }
    public MultiTextWriter(params TextWriter[] writers)
    {
        this.writers = writers;
    }

    public override void Write(char value)
    {
        foreach (var writer in writers)
            writer.Write(value);
    }

    public override void Write(string value)
    {
        foreach (var writer in writers)
            writer.Write(value);
    }

    public override void Flush()
    {
        foreach (var writer in writers)
            writer.Flush();
    }

    public override void Close()
    {
        foreach (var writer in writers)
            writer.Close();
    }

    public override Encoding Encoding
    {
        get { return Encoding.ASCII; }
    }
}

然后使用它我们可以做到:

Then using this we can do:

Console.SetOut(new MultiTextWriter(new ControlWriter(textbox1), Console.Out));

这篇关于将Console.WriteLine()重定向到文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆