[英]how to convert Console.WriteLine in to RichTextBox?
how to convert Console.WriteLine
to RichTextBox
? 如何将
Console.WriteLine
转换为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 我想知道是否有人知道并且我不知道(因为我提出的问题)......我搜索了一下,什么都没找到,所以我想知道你是否有人可以帮助我
You can use AppendText as below 您可以使用AppendText,如下所示
//Console.WriteLine("Hello");
richTextBox1.AppendText(String.Format("{0}{1}", "Hello", Environment.NewLine));
As per your comments you want to write text form windows form to a console application 根据您的意见,您希望将文本窗体窗体写入控制台应用程序
Add class called Win32 to your project like below 像下面一样将名为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();
}
}
You have to do few changes to windows form application like below 您必须对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.
}
Done! 完成!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.