指定全部显示不同颜色:

        public void SetTextContent(string text, ColorEnum color)
{
Font font = new Font("微软雅黑", , 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 = ,
Blue = ,
Red =
}

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

        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) > )
{
color = ColorEnum.Red;
var contentArray = text.Split(new[] { "#Red#" }, StringSplitOptions.None);
content = contentArray[];
var findstrs = contentArray[];
findList = GetFindList(findstrs);
}
else if (text.IndexOf("#Blue#", StringComparison.Ordinal) > )
{
color = ColorEnum.Blue;
var contentArray = text.Split(new[] { "#Blue#" }, StringSplitOptions.None);
content = contentArray[];
var findstrs = contentArray[];
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) > )
{
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("微软雅黑", , 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 = ;
while (start < content.Length)
{
int index = content.IndexOf(findStr, start, StringComparison.Ordinal);
if (index >= )
{
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用法——设置指定字符串的颜色

RichTextBox指定全部文字显示不同颜色及部分文字高亮颜色显示的更多相关文章

  1. js:鼠标移动到文字显示div,移出文字div显示,鼠标能移进div

    <!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8" ...

  2. [置顶] echarts x轴文字显示不全(xAxis文字倾斜比较全面的3种做法值得推荐)

    echarts x轴标签文字过多导致显示不全 如图: 解决办法1:xAxis.axisLabel 属性 axisLabel的类型是object ,主要作用是:坐标轴刻度标签的相关设置.(当然yAxis ...

  3. echarts x轴文字显示不全(解决方案)

    echarts x轴标签文字过多导致显示不全 如图: 解决办法1:xAxis.axisLabel 属性 axisLabel的类型是object ,主要作用是:坐标轴刻度标签的相关设置.(当然yAxis ...

  4. 设置UIButton中的文字和图片,设置UILabel的文在显示不同颜色

    UIButton: UIEdgeInsets 在UIButton中有三个对EdgeInsets的设置:ContentEdgeInsets.titleEdgeInsets.imageEdgeInsets ...

  5. C#+OpenGL+FreeType显示3D文字(3) - 用PointSprite绘制文字

    C#+OpenGL+FreeType显示3D文字(3) - 用PointSprite绘制文字 上一篇实现了把文字绘制到OpenGL窗口,但实质上只是把含有文字的贴图贴到矩形模型上.本篇我们介绍用Poi ...

  6. div内文字显示两行,多出的文字用省略号显示

    用-webkit-私有属性,代码如下:text-overflow: -o-ellipsis-lastline;overflow: hidden;text-overflow: ellipsis;disp ...

  7. php 两段文本对比,不同的文字显示高亮

    php 两段文本对比,不同的文字显示高亮[下面这个只能区分错误后面的..]   <?php $str1 ="MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwgg ...

  8. winform中dataGridView隔行显示不同的背景色,鼠标移动上显示不同颜色,离开后变回原色

    winform中dataGridView隔行显示不同的背景色,鼠标移动上显示不同颜色,离开后变回原色 先设置奇数行颜色,这个有个自带的属性AlternatingRowsDefaultCellStyle ...

  9. python: HTML之 鼠标放上去下拉项字体显示不同颜色

    鼠标放上去下拉项字体显示不同颜色 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "h ...

随机推荐

  1. DateNavigator

    <Border BorderThickness="1,1,1,1" BorderBrush="Black" Grid.Column="1&quo ...

  2. Iterable转List

    Iterable转List Iterable<Entity> geted = entityDao.findAll(); List<Entity> list = Lists.ne ...

  3. 正则 js截取时间

    项目中要把时间截取,只要年月日,不要时分秒,于是 /\d{4}-\d{1,2}-\d{1,2}/g.exec("2012-6-18 00:00:00") 或者另一种 var dat ...

  4. [Algorithm] Print All Subsets of a Set

    Let's say given a number of array, you should print out, all the subet of this array. Example: [1, 2 ...

  5. 【Java VisualVM】使用 VisualVM 进行性能分析及调优

    转载:https://blog.csdn.net/lmb55/article/details/79267277 一.概述 开发大型 Java 应用程序的过程中难免遇到内存泄露.性能瓶颈等问题,比如文件 ...

  6. 深入研究嵌入式web服务器的视频监控应用

    http://embed.chinaitlab.com/pc/776136.html uCLinux下,主要有3个Web Server:Httpd.Thttpd和BOA.Httpd是最简单的一个Web ...

  7. ubuntu 软件包管理工具 dpkg,apt-get,aptitude 区别

    ubuntu 软件包管理工具 dpkg,apt-get,aptitude 区别 一:dpkg dpkg 是一种比较低层的软件包安装管理工具,在安装时,不会安装软件包的依赖关系:只能安装所要求的软件包: ...

  8. mac 终端 使用 solarized 主题设置语法高亮

    mac 终端 使用 solarized 主题设置语法高亮 先来看看 solarized 在 mac 终端上的效果图片 一:先下载 solarized 官网下载:https://github.com/a ...

  9. JSP的页面连接和提交方式(web基础学习笔记六)

    一.GET请求新页面 1.1.超链接请求新页面 <!-- 超链接到page2 --> <a href="page2.jsp">链接到page2</a& ...

  10. An application icon

    The application icon is a small image which is usually displayed in the top left corner of the title ...