【问题标题】:WinForm richtextbox SelectionColor bugWinForm richtextbox SelectionColor bug
【发布时间】:2016-02-20 08:43:05
【问题描述】:

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

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

第一个代码结果:

其他代码,唯一改变的是“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");
            }
        }

第二个代码结果:

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

【问题讨论】:

    标签: c# winforms richtextbox


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-14
      • 1970-01-01
      • 2015-06-26
      • 2011-03-07
      • 1970-01-01
      • 1970-01-01
      • 2019-11-18
      • 1970-01-01
      相关资源
      最近更新 更多