WinForm richtextbox SelectionColor 错误

WinForm richtextbox SelectionColor bug

我有一个 winform 项目,使用 richtextbox。代码;

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:

其他代码,唯一改变的是 "S" 而不是“Ş”:

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?

您的代码适用于我的示例,但我们可能有不同的 .NET 版本、环境等。请尝试此代码,它将选择边界设置为适当的值,应该可以解决您在示例结果中显示的问题:

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);

如果默认使用黑色,而您只想将某些部分标记为红色,则此代码可以重构为:

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");
}