分享

c#中richtextbox显示不同的颜色

 长江黄鹤 2016-07-25

List strWarn=new List();//定义一个泛型,可以自由的添加字段

private void WarnInfo(string str)
        {
            strWarn.Add(str);
            for (int i = 0; i < strWarn.Count; i++)
            {
                int lenth = richTextBox1.Text.Length;
                richTextBox1.AppendText(strWarn[i]);
                if (richTextBox1.Lines[i].Contains("一级警告"))
                {
                    for (int j = 0; strWarn[i][j] != '\n'; j++)//对于这一行来说\n是结尾
                    {
                        richTextBox1.Select(lenth + j, 1);//一个一个的选择然后改变颜色
                        richTextBox1.SelectionColor = Color.Red;
                    }
                }
                if (richTextBox1.Lines[i].Contains("二级警告"))
                {
                    for (int j = 0; strWarn[i][j] != '\n'; j++)
                    {
                        richTextBox1.Select(lenth + j, 1);
                        richTextBox1.SelectionColor = Color.Orange;
                    }
                }
           }
        }

当需要滚动显示出来的信息都是显示最后的信息可以附加:

private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            richTextBox1.SelectionStart = richTextBox1.TextLength;
            // Scrolls the contents of the control to the current caret position.
            richTextBox1.ScrollToCaret();

        }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多