简体   繁体   English

在RichTextBox中添加更多文本并突出显示时,它将清除所有以前的突出显示

[英]When adding more text into a RichTextBox and highlighting, it clears all previous highlights

I'm making a debug/log form that saves all the things done in the others forms at the specific time: like pressing a button, entering some info in textbox, etc. 我正在制作一个调试/日志表单,该表单将在特定时间保存其他表单中完成的所有工作:例如按下按钮,在文本框中输入一些信息等。

In every line I highlight the actual time that corresponds to the same day, for example, today is 10/08/2019 so: 在每一行中,我突出显示与当天相对应的实际时间,例如,今天是10/08/2019,因此:

Example 1 例子1

10/08/2019 corresponds to the actual date, so it's highlighted. 10/08/2019与实际日期相对应,因此已突出显示。 The problem is that when I do another thing like pressing L button it shows like this: 问题是当我做其他事情,例如按L按钮时,它显示如下:

Example 2 例子2

Below I put the code that it interprets this: CMD = RichTextBox 下面,我将其解释为以下代码:CMD = RichTextBox

private void ChequearDatos()
{
    string line = CMD.Text;
    int x = xk, xx = 0, lent = 0;
    lent = line.Length;

    do
    {
        else if (line[x] == '\n')
        {
            xk = x;                   
            x++;
        }

        else if (line[x] == '■')
        {
            xx = x + 1;
            do
            {
                xx = xx + 1;
            }
            while (line[xx] != '=');
            string pedazo = line.Substring(x + 2, (xx - x) - 12);
            if (pedazo == Convert.ToString(DateTime.Today.Day + "/" + DateTime.Today.Month + "/" + DateTime.Today.Year))
            {
                CMD.SelectionStart = x;
                CMD.SelectionLength = xx - x + 1;
                CMD.SelectionColor = System.Drawing.Color.OrangeRed;
            }
            else
            {
                CMD.SelectionStart = x;
                CMD.SelectionLength = xx - x + 1;
                CMD.SelectionColor = System.Drawing.Color.DarkKhaki;
            }
            CMD.SelectionStart = CMD.TextLength;

            CMD.ScrollToCaret();
            if (xx + 1 > lent) { break; }
            else { x = xx + 1; } 
        }
        else { x = x + 1; }
    }
    while (x <= lent - 1);
}

The program search for ■ and =, the inside of it if it's the same date it will be highlighted with DarkKhaki colour, if not, it will be highlighted with OrangeRed colour. 程序将搜索■和=,如果日期相同,则将其内部用DarkKhaki颜色突出显示;如果不是同一日期,则将使用OrangeRed颜色突出显示。

In the first run, it runs good, but when I add more text and call that function again it highlight everything in white except the last message. 在第一次运行中,它运行良好,但是当我添加更多文本并再次调用该函数时,除最后一条消息外,所有内容均以白色突出显示。

Edit: I tried without saving the xk int variable, but when I call this function the program needs to process all the text again and starts to blink until checked all of it. 编辑:我尝试过不保存xk int变量,但是当我调用此函数时,程序需要再次处理所有文本,并开始闪烁直到选中所有文本。

I fixed it appending it like @TnTinMin said, before I call this function another function writes in the RTB as below: 我像@TnTinMin所说的那样固定了它,在我调用此函数之前,另一个函数在RTB中写入如下:

void PrintRTB(string a)
{
  RTB.text += "■ " +  a + "\n";
  ChequearDatos();
}

Now I use the command [.appendtext] and works! 现在,我使用命令[.appendtext]可以工作了!

void PrintRTB(string a)
{
  RTB.AppendText("■ " + a + "\n");
  ChequearDatos();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM