指定全部显示不同颜色:

        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】Python 微服务框架 nameko

    nameko: 1.支持服务发现.负载均衡 2.支持依赖自动注入,使用很方便 3.缺点:超时.限速.权限等机制不完善 代码示例:https://github.com/junneyang/nameko- ...

  2. 机器视觉之 ICP算法和RANSAC算法

    临时研究了下机器视觉两个基本算法的算法原理 ,可能有理解错误的地方,希望发现了告诉我一下 主要是了解思想,就不写具体的计算公式之类的了 (一) ICP算法(Iterative Closest Poin ...

  3. Android的View和ViewGroup分析

    1. 概念 Android中的View与我们曾经理解的"视图"不同.在Android中,View比视图具有更广的含义,它包括了用户交互和显示,更像Windows操作系统中的wind ...

  4. Jmeter-Maven-Plugin高级应用:Proxy Configuration

    Proxy Configuration Pages 12 Home Adding additional libraries to the classpath Advanced Configuratio ...

  5. (LeetCode 160)Intersection of Two Linked Lists

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  6. Postgresql监控pgwatch的搭建

    一,需要环境: You will need a handful of components to make this work: - Apache (webserver) #apache搭建web页面 ...

  7. JBOSS启动报错解决方案

    同一个jboss下不可以放不同的项目包,否则报错: 注意:如果后期使用,注意删除上图的本地文件,重新加载即可.

  8. JSP实现数据保存(web基础学习笔记四)

    session对象: //服务端设置Session属性 session.setAttribute("user", username); //客户端接收Session Object ...

  9. mysql循环批量插入测试数据

    http://blog.51cto.com/tianxingzhe/1676097 DROP PROCEDURE test_insert ; DELIMITER $$ CREATE PROCEDURE ...

  10. 【FinacialKnowledge】财务报表及名词解释

    1.财务报表 以下三张表为:资产负债表.利润表.现金流量表                                                                        ...