0

how to convert Console.WriteLine to RichTextBox?

I was wondering if anyone knew and I do not know (for something I put the question) ... I searched and found nothing so I want to know if any of you can help me

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

4

1 回答 1

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.
    }

完毕!

在此处输入图像描述

参考这篇博文了解更多信息

于 2013-06-01T06:17:05.187 回答