在winform上想要一个类似控制台一样的输入工具 ,把空间richTextBox 做下调整就可以实现。
1. 在窗体上搞一个richTextBox 控件 ,然后再窗体初始化函数或者 load函数中写
this.richTextBox1.HideSelection = false;
2. 编写 richTextBox 的TextChanged事件
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
//this.richTextBox1.re
int len = richTextBox1.Lines.Length;
if (len > 200)
{
List<string> lines = richTextBox1.Lines.ToList();
lines.RemoveRange(0, len - 200);
this.richTextBox1.Lines = lines.ToArray();
this.richTextBox1.SelectionStart= richTextBox1.TextLength;
}
}
3. 最重要的一点 ,再添加内容时,需要使用 AppendText ,而不是Test
private void printLogTextConsole(string message,string leven)
{
string log = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")+" | "+leven.ToUpper() + " | " + message+"\r\n";
richTextBox1.AppendText(log);
}