WinForm richtextbox SelectionColor bug

时间:2016-02-20 08:43:05

标签: c# winforms richtextbox

我有一个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");
            }
        }
  

第二代码结果:

enter image description here

  

为什么第二行中第二行的黑色“Ş”字符?有什么问题,不支持我的文化,还是在richtextbox中有任何错误?

1 个答案:

答案 0 :(得分:0)

您的代码适用于我的示例,但我们可能有不同的.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");
}