利用WinForm的textBox实现控制台的Console.WriteLine

本文介绍了一种方法,可以将C#程序中的Console.WriteLine输出重定向到Windows Forms应用程序的TextBox控件中。通过继承TextWriter类并重写Write和WriteLine方法实现,此方法允许在不依赖于控制台的情况下显示输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

重写WriteLine与Write方法,重写后Console.WriteLine将直接写到Winform的textBox框中,不受类库的限制。


public class ConsoleHelper : TextWriter
{
    private System.Windows.Forms.TextBox _textBox { set; get; }
    private int maxRowLenght = 200;//textBox中显示的最大行数,若不限制,则置为0
    public ConsoleHelper(System.Windows.Forms.TextBox textBox)
    {
        this._textBox = textBox;
        Console.SetOut(this);
    }
     public override void Write(string value)
    {
        if (_textBox.IsHandleCreated)
            _textBox.BeginInvoke(new ThreadStart(() => 
			{
				if(maxRowLenght > 0 && _textBox.Lines.Lenght > maxRowLenght)
				{
					int strat = _textBox.GetFirstCharIndexFromLine(0);//获取第0行第一个字符的索引
					int end = _textBox.GetFirstCharIndexFromLine(10);
					_textBox.Select(strat,end);//选择文本框中的文本范围
					_textBox.SelectedText = "";//将当前选定的文本内容置为“”
					_textBox.AppendText(value + " ");
				}
				else
				{
					_textBox.AppendText(value + " ");
				}
			}));
    }

    public override void WriteLine(string value)
    {
        if (_textBox.IsHandleCreated)
            _textBox.BeginInvoke(new ThreadStart(() => 
			{
				if(maxRowLenght > 0 && _textBox.Lines.Lenght > maxRowLenght)
				{
					int strat = _textBox.GetFirstCharIndexFromLine(0);//获取第0行第一个字符的索引
					int end = _textBox.GetFirstCharIndexFromLine(10);
					_textBox.Select(strat,end);//选择文本框中的文本范围
					_textBox.SelectedText = "";//将当前选定的文本内容置为“”
					_textBox.AppendText(value + "\r\n");
				}
				else
				{
					_textBox.AppendText(value + "\r\n");
				}
			}));
    }

    public override Encoding Encoding//这里要注意,重写wirte必须也要重写编码类型
    {
        get { return Encoding.UTF8; }
    }
}
然后在Form.cs中的Form()中初始化一下:
public Form()
{
	InitializeComponent();
	new ConsoleHelper(txt_this);//重写Console的Write与WriteLine
}
txt_this为要输出的目标textbox。
不想再向Form输出时,直接屏蔽 new ConsoleHelper(txt_this);

那么现在就可以在任何地方使用Console.WriteLine()来输出到Form中的textBox中了。


在C#中,如果你想在一个Windows Form应用程序(WinForm)向控制台应用程序传递参数,并让控制台应用程序返回结果给WinForm,通常会通过以下步骤: 1. **创建控制台项目**:首先,你需要创建一个新的控制台应用程序项目。在该项目中,你可以编写接收和处理参数的代码。 ```csharp // 控制台App.cs using System; class Program { public static string ProcessParams(string input) { // 在这里处理输入并生成输出 return "处理后的结果"; } static void Main(string[] args) { if (args.Length > 0) { Console.WriteLine("接收到的参数: " + args[0]); string result = ProcessParams(args[0]); Console.WriteLine("输出结果: " + result); } else { Console.WriteLine("请输入参数:"); } Console.ReadLine(); // 等待用户关闭控制台窗口 } } ``` 2. **从WinForm调用控制台程序**:在WinForm中,你可以使用Process类的Start方法启动控制台程序,并传递参数。例如: ```csharp using System.Diagnostics; // WinForm1.cs private void buttonSubmit_Click(object sender, EventArgs e) { ProcessStartInfo psi = new ProcessStartInfo(); psi.FileName = "控制台App.exe"; // 控制台程序路径 psi.Arguments = textBoxInput.Text; // 从控件获取输入参数 using (Process process = Process.Start(psi)) { process.WaitForExit(); // 等待控制台进程结束 // 获取控制台输出 if (process.StandardOutput != null) { string output = process.StandardOutput.ReadToEnd(); MessageBox.Show(output, "控制台输出"); } } } ``` 当你点击WinForm的按钮时,WinForm会将文本框中的内容作为参数发送给控制台程序,并显示其输出。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值