• RichTextBox指定全部文字显示不同颜色及部分文字高亮颜色显示


    指定全部显示不同颜色:

            public void SetTextContent(string text, ColorEnum color)
            {
                Font font = new Font("微软雅黑", 14, FontStyle.Bold);
                richTextBox1.Font = font;
                richTextBox1.Text = text;
                richTextBox1.ReadOnly = true;
                layoutControlItem2.Selected = true;
                SendKeys.Send("{Tab}");  //向活动应用程序发送击键 注意格式:Send("{Tab}");中的{}
                switch (color)
                {
                    case ColorEnum.Blue:
                        this.Text = "警告信息";
                        richTextBox1.ForeColor = Color.Blue;
                        break;
                    case ColorEnum.Red:
                        this.Text = "错误信息";
                        richTextBox1.ForeColor = Color.Red;
                        break;
                    default:
                        this.Text = "提示信息";
                        richTextBox1.ForeColor = Color.Black;
                        break;
                }
            }
        public enum ColorEnum
        {
            Black = 0,
            Blue = 1,
            Red = 2
        }

    指定内容中指定文字高亮显示:

            private void SetHighlightContent(string text)
            {
                ColorEnum color = ColorEnum.Black;
                string content = string.Empty;
                List<string> findList = new List<string>();
                if (text.IndexOf("#Red#", StringComparison.Ordinal) > 0)
                {
                    color = ColorEnum.Red;
                    var contentArray = text.Split(new[] { "#Red#" }, StringSplitOptions.None);
                    content = contentArray[0];
                    var findstrs = contentArray[1];
                    findList = GetFindList(findstrs);
                }
                else if (text.IndexOf("#Blue#", StringComparison.Ordinal) > 0)
                {
                    color = ColorEnum.Blue;
                    var contentArray = text.Split(new[] { "#Blue#" }, StringSplitOptions.None);
                    content = contentArray[0];
                    var findstrs = contentArray[1];
                    findList = GetFindList(findstrs);
                }
                SetHighlight(content, findList, color);
            }
    
            /// <summary>
            /// 多个要高亮关键词显示的用'&'连接
            /// </summary>
            /// <param name="findstrs"></param>
            /// <returns></returns>
            private List<string> GetFindList(string findstrs)
            {
                List<string> findList;
                if (findstrs.IndexOf("&", StringComparison.Ordinal) > 0)
                {
                    findList = findstrs.Split('&').ToList();
                }
                else
                {
                    findList = new List<string>() { findstrs };
                }
                return findList;
            }
    
            private void SetHighlight(string content, List<string> findList, ColorEnum color)
            {
                if (!string.IsNullOrEmpty(content))
                {
                    if (findList.IsHasRow())
                    {
                        Font font = new Font("微软雅黑", 14, FontStyle.Bold);
                        richTextBox1.Font = font;
                        richTextBox1.Text = content;
                        this.Text = @"提示信息";
                        richTextBox1.ReadOnly = true;
                        foreach (var findItem in findList)
                        {
                            List<int> findStrIndexes = GetFindStrIndexes(content, findItem);
                            foreach (var itemindex in findStrIndexes)
                            {
                                richTextBox1.Select(itemindex, findItem.Length);
                                switch (color)
                                {
                                    case ColorEnum.Blue:
                                        richTextBox1.SelectionColor = Color.Blue;
                                        break;
                                    case ColorEnum.Red:
                                        richTextBox1.SelectionColor = Color.Red;
                                        break;
                                }
                            }
                        }
                        richTextBox1.SelectionStart = richTextBox1.Text.Length; //取消选中
                        layoutControlItem2.Selected = true;
                        SendKeys.Send("{Tab}"); //向活动应用程序发送击键 注意格式:Send("{Tab}");中的{}
                    }
                    else
                    {
                        SetTextContent(content, color);
                    }
                }
            }
    
            private List<int> GetFindStrIndexes(string content, string findStr)
            {
                List<int> result = new List<int>();
                int start = 0;
                while (start < content.Length)
                {
                    int index = content.IndexOf(findStr, start, StringComparison.Ordinal);
                    if (index >= 0)
                    {
                        result.Add(index);
                        start = index + findStr.Length;
                    }
                    else
                    {
                        break;
                    }
                }
                return result;
            }

    参考:

    http://www.cnblogs.com/KardelXiao/p/4236045.html (C#)RichTextBox控件 链接跳转设置
    http://blog.csdn.net/crazytaliban/article/details/52002657 RichTextBox用法——设置指定字符串的颜色

  • 相关阅读:
    使用Bootstrap后,关于IE与Chrome显示字体的问题
    利用百度接口,识别身份证
    双日历日期选择控件
    回复一个朋友:如何理解委托
    IIS7增加mine类型,以便可以访问apk
    关于SqlBulkCopy SQL批量导入需要注意,列名是区分大小写的
    关于取表中id最大值+1的select语句,哪种效率更高?
    MySQL中如何分析查询语句
    判断同名股票是否存在的MyBatis查询函数写法
    Thymeleaf中model设一个值 页面显示此值 JS取此值
  • 原文地址:https://www.cnblogs.com/shy1766IT/p/8294759.html
Copyright © 2020-2023  润新知