![](/img/trans.png)
[英]Convert console.writeline to RichTextBox.AppendText() c#
[英]how to convert Console.WriteLine in to RichTextBox?
如何将Console.WriteLine
转换为RichTextBox
?
我想知道是否有人知道并且我不知道(因为我提出的问题)......我搜索了一下,什么都没找到,所以我想知道你是否有人可以帮助我
您可以使用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.
}
完成!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.