指定全部显示不同颜色:

        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. svn 错误集锦续

    4,svn出错:Error: File or directory '.' is out of date; try updating 出错原因:SVN服务器端的版本比你的版本要新,不允许提交. 解决方案 ...

  2. python 抓取alexa数据

    要抓取http://www.alexa.cn/rank/baidu.com网站的排名信息:例如抓取以下信息: 需要微信扫描登录 因为这个网站抓取数据是收费,所以就利用网站提供API服务获取json信息 ...

  3. 【华为OJ平台练习题】求最大公共子串的个数和元素

    1.原题是求出最大公共子串的个数就可以 原理:利用二维矩阵排列的方式.将俩字符串进行比較 #include <iostream> #include <vector> using ...

  4. 微信小程序价值思考:手机端的CS-BS迁移

    从很多特点来看,小程序都非常类似于网页:主要的业务逻辑在服务端.客户端无需安装应用程序.小程序的开发采用的HTML+JS+CSS技术等等.张小龙自己对小程序的定位也大概如此:无意做小程序分发平台,只是 ...

  5. sqlplus的使用

    1.连接数据库 sqlplus / as sysdba 2.连接到远程数据库 sqlplus 用户名/密码@服务命名 3.遇到&会当成变量,一般是不需要的,可以关掉 SQL> set d ...

  6. Android 四大组件之 Service(二)

    这里主要介绍Service组件的使用. 1.定义一个继承Service的子类 如下: package com.service; import android.app.Service; import a ...

  7. Test Double

    我不知道Test Double翻译成中文是什么,测试替身?Test Double就像是陈龙大哥电影里的替身,起到以假乱真的作用.在单元测试时,使用Test Double减少对被测对象的依赖,使得测试更 ...

  8. linux sar命令详细说明相关参数

    详细说明linux的sar命令 sar(System Activity Reporter系统活动情况报告)是目前 Linux 上最为全面的系统性能分析工具之一,可以从多方面对系统的活动进行报告,包括: ...

  9. [Done]Spring @Pointcut 切点调用不到(SpringAOP嵌套方法不起作用) 注意事项

    今天在开发过程中,遇到一个问题卡了很久,测试代码如下: package spring.pointcut; import org.aspectj.lang.ProceedingJoinPoint; im ...

  10. JProfiler 解决 Java 服务器的性能跟踪

    作者:徐建祥(netpirate@gmail.com) 时间: 2006/01/05 来自:http://www.anymobile.org 1.摘要......................... ...