Redirecting Console.WriteLine() to Textbox
I'm building this application in Visual Studio 2010 using C#.
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.
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).
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.
Thanks in advance!
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.
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; }
    }
}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));Redirecting Console.WriteLine() to Textbox的更多相关文章
- 你们信不信一句Console.WriteLine就能让你的控制台程序失去响应
		好久没更新博客了,今天是扒衣见君节,难得闲下来就来说说一个最近有趣的发现吧. 首先废话不多说,直接上代码吧 class Program { static void Main(string[] args ... 
- VS2015使用技巧 为什么我们可以输入cw后按两下tab键出现console.writeline
		镇场诗: 大梦谁觉,水月中建博客.百千磨难,才知世事无常. 今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ 为什么 ... 
- C# Winform里面用Console.WriteLine输出到哪了
		C# Winform里面用Console.WriteLine输出也不会报错 显示在 VS IDE 的视图→输出窗口,且只在 Debug 环境下此语句执行. 如果是 Release 环境,在 Win32 ... 
- Console.WriteLine()与MessageBox.Show()的区别
		Console.WriteLine(); 将当前行终止符写入标准输出流 在学习控制台应用程序时经常用,输出到控制台 MessageBox.Show(); 显示可包含文本.按钮和符号(通知并指示用户) ... 
- 第一个输出程序 Console.WriteLine
		using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ... 
- C#中Console.WriteLine()函数输出格式详解
		格式项都采用如下形式: {index[,alignment][:formatString]} 其中"index"指索引占位符,这个肯定都知道: ",alignment&q ... 
- 看到Console.WriteLine($"string")写法,一时间不理解$的用途
		参了网上资料,原来它是C# 6.0的语法糖. C# 6.0 新加上的功能: Null-Conditional Operator 大概就是,简洁代码量,缩短一些关于为null的判断~ 旧写法: pu ... 
- Console.WriteLine的小用法
		我在一开始使用Console.WriteLine的时候,经常采用的是拼接字符串的形式来构建输出. 但是Console.WriteLine具有扩展的方法来对内容进行输出,类似于我们常用的String.F ... 
- 【C#小知识】C#中一些易混淆概念总结(四)---------解析Console.WriteLine()                                                       分类:            C#             2014-02-05 17:18    1060人阅读    评论(0)    收藏
		目录: [C#小知识]C#中一些易混淆概念总结 [C#小知识]C#中一些易混淆概念总结(二) [C#小知识]C#中一些易混淆概念总结(三) ------------------------------ ... 
随机推荐
- Node.js 事件
			Node.js 事件 Node.js 所有的异步I/O 操作在完成时都会发送一个事件到事件队列. Node.js里面的许多对象都会分发事件:一个net.Server对象会在每次有新连接时分发一个事件, ... 
- (转)jQuery Mobile 移动开发中的日期插件Mobiscroll 2.3 使用说明
			(原)http://www.cnblogs.com/hxling/archive/2012/12/12/2814207.html jQuery Mobile 移动开发中的日期插件Mobiscroll ... 
- SVG 2D入门12 - SVG DOM
			使用脚本可以很方便的完成各种复杂的任务,也是完成动画和交互的一种主流方式.由于SVG是html的元素,所以支持普通的DOM操作,又由于SVG本质上是xml文档,所以也有一种特殊的DOM操作,大多称之为 ... 
- eclipse working sets 视图 解决Other Projects不见问题
			请移步: http://note.youdao.com/yws/public/redirect/share?id=d54cac4232078f9acab551d62337a2d1&type=f ... 
- ACM感悟
			声明:本文是写给弱校ACM新手的一点总结,受自身水平和眼界所限,难免会有一些个人主观色彩,希望大牛指正 感谢@Wackysoft .@哇晴天 . @ 一切皆有可能1 的指教,现根据这些建议,文章已进行 ... 
- OD调试篇6--对一些真正的小程序进行一点点的修改
			先打开这个程序看看,提醒你这是一个未注册版本的软件.会发现只能添加4个联系人,这显然是我不想看见的,于是我要对这个程序进行一些修改,嘿嘿... 通过OD载入这个程序 有一些(SEH)也就是异常,我们可 ... 
- Word或者Excel中怎么把 "空格" 替换成 "换行 "
			word中ctrl+h打开替换,将" "替换为^pexcel替换成alt+小键盘区的10 
- thrift编译安装
			关于thrift的介绍:http://www.ibm.com/developerworks/cn/java/j-lo-apachethrift/ Apache Thrift 是 Facebook 实现 ... 
- 由STL所想到的  C++显示调用析构函数
			在STL中的容器中的析构函数中,会经常调用destroy()这个函数 在STL中 destroy()被重载了 这点在这里到不去讨论 这里贴最简单的那个版本 template<class T&g ... 
- 关于路径的使用,assi下载和
			直接给一个路径下载图片,这函数直接使用assi -(void)downloadWithURL:(NSString*)RequestUrl SavePath:(NSString*)savepath wi ... 
