C# 我正在使用richTextBox查看日志,但如何使它不会每次下移到新行?

C# 我正在使用richTextBox查看日志,但如何使它不会每次下移到新行?,c#,C#,这是我在计时器滴答声事件中使用它的方式: private void timer1_Tick(object sender, EventArgs e) { memUsage = theMemCounter.NextValue(); label1.Text = memUsage.ToString(); Logger.Write("Memory Usage " + memUsage.ToString());

这是我在计时器滴答声事件中使用它的方式:

private void timer1_Tick(object sender, EventArgs e)
        {
            memUsage = theMemCounter.NextValue();
            label1.Text = memUsage.ToString();
            Logger.Write("Memory Usage   " + memUsage.ToString());
            AppendText(richTextBox1, "Memory Usage   " + memUsage.ToString() + Environment.NewLine, Color.Red);
            cpuUsage = this.theCPUCounter.NextValue();
            label2.Text = cpuUsage.ToString();
            Logger.Write("Cpu Usage   " + this.cpuUsage.ToString());
            AppendText(richTextBox1, "Cpu Usage   " + cpuUsage.ToString() + Environment.NewLine, Color.Blue);
            Values.Add(cpuUsage);
            isProcessRunning();
            if (alreadyRun == true)
            {
                processValues.Add(cpuUsage);
            }

        }
这是AppendText函数:

public  void AppendText( RichTextBox box, string text, Color color)
        {
            box.SelectionStart = box.TextLength;
            box.SelectionLength = 0;

            box.SelectionColor = color;
            box.AppendText(text);
            box.SelectionColor = box.ForeColor;
        } 
若在计时器中,我将不使用Environment.NewLine,那个么我将只看到其中一个内存使用情况,一行重复它自己

如果我使用Environment.NewLine,那么每次我都会看到内存使用率和cpu使用率下降到一个新行,例如:

Moemory用法--3 Cpu使用率--0.4565 内存使用率--3.6 Cpu使用率--47


相反,我希望看到一行内存使用情况和一行cpu参数,并反复更新这两行,而不是为它们创建新的行。

如果您试图只更新两行,请尝试以下操作:

private void timer1_Tick(object sender, EventArgs e)
{
    richTextBox1.Text = "";
    // Add the rest of the code here
}
这样,它将显示2行(内存和cpu使用),在打勾后,它将清除文本,然后设置这2行。这将显示为正在更新


希望这有帮助

你为什么要用RichtextBox?L.B这样我就可以给文本的线条上色了。那我应该用什么?