指定全部显示不同颜色:

        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. python 斐波拉契数列数列

    '''斐波拉契数列'''def Fibonacci(n): first, next = 0, 1 i = 0; while i < n: print next first, next = nex ...

  2. 网络结构设计——负载均衡之LVS学习笔记(四)

    LVS按个人理解的说就是将一台Linux服务器当作路由器等功能的技术.LVS---Linux虚拟服务器. LVS实现了三种IP负载均衡技术VS/NAT.VS/TUN.VS/DR. 今天简单分享一下我在 ...

  3. [Javascript] Coding interview problem: Scheduler functional way

    Implement a job scheduler which takes in a function f and an integer n, and calls f after nmilliseco ...

  4. 代码录播:jQueryMobile 实现一个简单的弹出框效果

    今天给大家带来的是 jQueryMobile 实现一个简单的弹出框效果,有兴趣的童鞋可以试试哦~ ^_^ 阅读原文:www.gbtags.com  

  5. @QueryParam和@PathParam使用方法比较

    1 先来看@queryparam Path("/users") public class UserService { @GET @Path("/query") ...

  6. 算法笔记_112:第五届蓝桥杯软件类省赛真题(Java本科B组)试题解答

     目录 1 武功秘籍 2 切面条 3 猜字母 4 大衍数列 5 圆周率 6 奇怪的分式 7 扑克序列 8 分糖果 9 地宫取宝 10 矩阵翻硬币   前言:以下试题解答代码部分仅供参考,若有不当之处, ...

  7. Linux 小知识点

    1:当前目录比较重要的几个文件 . .. anaconda-ks.cfg .bash_history .bash_logout .bash_profile .bashrc .cshrc install ...

  8. eclipse 打包 apk 文件

    1.通过eclipse中的file ,点击Import 导入项目 2.选择Android 中的 Existing Android Code Into Workspace 3.通过 Brower 引入项 ...

  9. html中title属性换行实现

    本文主要讲解titile属性换行的实现方式.<title> 元素可定义文档的标题,可以应用在img.a等标签上. 1.实现方式: <!DOCTYPE html> <htm ...

  10. break的使用方法

    private static void test() {  for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { Syst ...