日期:2014-05-18  浏览次数:21122 次

可以将console.write里的数据显示在winform的textbox里?
请问一下要是在consoleapplication加入new item windowform,怎样可以将consolewrite的数据 显示在winform的textbox里?
拜托大家,帮帮忙!!!

------解决方案--------------------
刚才试了试,代码如下
C# code

        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Form1 frm1 = new Form1();
            frm1.Show();

            Console.Write("abc");
            Console.WriteLine("abc");
        }

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            ConsoleRedir();
        }
        
        public void ConsoleRedir()
        {
            StringRedir r = new StringRedir(ref textBox1);
            
            Console.SetOut(r);
        }

        public class StringRedir : StringWriter
        {
            private TextBox outBox;

            public StringRedir(ref TextBox textBox)
            {
                outBox = textBox;
            }

            public override void WriteLine(string x)
            {
                outBox.Text += x + "\r\n";
                outBox.Refresh();
            }

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