如何将Console.WriteLine转换为RichTextBox?

时间:2013-06-01 06:03:18

标签: c# textbox console

如何将Console.WriteLine转换为RichTextBox

我想知道是否有人知道并且我不知道(因为我提出的问题)...我搜索并发现什么,所以我想知道你是否有人可以帮助我

http://i41.tinypic.com/9hutzo.png

1 个答案:

答案 0 :(得分:2)

您可以使用下面的AppendText

//Console.WriteLine("Hello");

richTextBox1.AppendText(String.Format("{0}{1}", "Hello", Environment.NewLine));

修改

根据您的意见,您希望将文本窗体窗体写入控制台应用程序

将名为Win32的类添加到您的项目中,如下所示

using System;
using System.Runtime.InteropServices;

namespace SoTest
{
    public class Win32
    {
        /// <summary>
        /// Allocates a new console for current process.
        /// </summary>
        [DllImport("kernel32.dll")]
        public static extern Boolean AllocConsole();

        /// <summary>
        /// Frees the console.
        /// </summary>
        [DllImport("kernel32.dll")]
        public static extern Boolean FreeConsole();
    }
}

您必须对Windows窗体应用程序进行一些更改,如下所示

   public Form1()
    {
        InitializeComponent();
        Win32.AllocConsole();  //Allocates a new console for current process.
    }

    private void button1_Click(object sender, EventArgs e) // on button click we write text to console 
    {
        Console.WriteLine(richTextBox1.Text); // write RTB text to console 
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)  // add form closing event 
    {
        Win32.FreeConsole(); // Free the console.
    }

完成!

enter image description here

Refere This blog Post for more information