简体   繁体   中英

WinForm richtextbox SelectionColor bug

I have a winform project, using richtextbox. The code;

List<string> list = new List<string>();
        list.Add("S\nS");
        list.Add("*S\nS");
        list.Add("S\nS");
        list.Add("*S\nS");

        for (int s = 0; s < list.Count; s++)
        {
            if (list.ElementAt(s)[0] == '*')
            {
                richTextBox1.SelectionColor = Color.Red;
                richTextBox1.AppendText(list.ElementAt(s).Substring(1, list.ElementAt(s).Length - 1));
                if (s != list.Count - 1) richTextBox1.AppendText("\r\n\r\n");
            }
            else
            {
                richTextBox1.SelectionColor = Color.Black;
                richTextBox1.AppendText(list.ElementAt(s));
                if (s != list.Count - 1) richTextBox1.AppendText("\r\n\r\n");
            }
        }

First code result:

第一个代码结果

Other code, The only thing changing "S" instead of "Ş":

List<string> list = new List<string>();
        list.Add("Ş\nŞ");
        list.Add("*Ş\nŞ");
        list.Add("Ş\nŞ");
        list.Add("*Ş\nŞ");

        for (int s = 0; s < list.Count; s++)
        {
            if (list.ElementAt(s)[0] == '*')
            {
                richTextBox1.SelectionColor = Color.Red;
                richTextBox1.AppendText(list.ElementAt(s).Substring(1, list.ElementAt(s).Length - 1));
                if (s != list.Count - 1) richTextBox1.AppendText("\r\n\r\n");
            }
            else
            {
                richTextBox1.SelectionColor = Color.Black;
                richTextBox1.AppendText(list.ElementAt(s));
                if (s != list.Count - 1) richTextBox1.AppendText("\r\n\r\n");
            }
        }

Second Code Result:

在此处输入图片说明

Why is black "Ş" characters of The second lines in second code? What is the problem, does not supported my culture or are there any bug in richtextbox?

Your code works on my sample but we may have different .NET versions, environment and so on. Please try this code, it sets the selection boundaries to appropriate values and should solve the problem you have shown in the example results:

List<string> list = new List<string>();
list.Add("Ş\nŞ");
list.Add("*Ş\nŞ");
list.Add("Ş\nŞ");
list.Add("*Ş\nŞ");

for (int s = 0; s < list.Count; s++)
{
    var diffColor = list.ElementAt(s)[0] == '*';
    var txtLength = richTextBox1.Text.Length;
    var myString = diffColor ? list.ElementAt(s).Substring(1, list.ElementAt(s).Length - 1) : list.ElementAt(s);

    richTextBox1.AppendText(myString);
    richTextBox1.SelectionStart = txtLength;
    richTextBox1.SelectionLength = myString.Length;
    richTextBox1.SelectionColor = diffColor ? Color.Red : Color.Black;

    if (s != list.Count - 1) richTextBox1.AppendText("\r\n\r\n");
}

richTextBox1.Select(0,0);

If black is used by default and you just want to mark certain parts to red this code may be refactored to:

List<string> list = new List<string>();
list.Add("Ş\nŞ");
list.Add("*Ş\nŞ");
list.Add("Ş\nŞ");
list.Add("*Ş\nŞ");

for (int s = 0; s < list.Count; s++)
{
    var diffColor = list.ElementAt(s)[0] == '*';
    var txtLength = richTextBox1.Text.Length;
    var myString = diffColor ? list.ElementAt(s).Substring(1, list.ElementAt(s).Length - 1) : list.ElementAt(s);

    richTextBox1.AppendText(myString);

    if (diffColor)
    {
        richTextBox1.SelectionStart = txtLength;
        richTextBox1.SelectionLength = myString.Length;
        richTextBox1.SelectionColor = Color.Red;
        richTextBox1.Select(0, 0);
    }

    if (s != list.Count - 1) richTextBox1.AppendText("\r\n\r\n");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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