富文本框 - 文本着色错误

时间:2011-03-16 12:44:46

标签: c# .net winforms richtextbox

我写了这个函数

    private void richAdd(string who, string what)
    {

        string colorstring = who + " ( " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + " ) :";

        richTextBox1.Text += colorstring + " " + what + "\r\n\r\n";
richTextBox1.DeselectAll();
        richTextBox1.Select(richTextBox1.Find(colorstring), colorstring.Length);

        richTextBox1.SelectionColor = Color.Blue;
richTextBox1.DeselectAll();
    }

应该用蓝色标记谁+时间,用黑色标记什么

然而,在第二次使所有文字变成蓝色之后......任何想法都会出现问题吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

    private void richAdd(string who, string what)
    {
        string colorstring = who + " ( " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + " ) :";
        richTextBox1.AppendText(colorstring + " " + what + "\r\n\r\n");
        richTextBox1.Select(richTextBox1.Text.LastIndexOf(colorstring), colorstring.Length);
        richTextBox1.SelectionColor = Color.Blue;
    }