参考了三份有用的资料:

1.关于richtextbox设置字体颜色的问题

http://biancheng.dnbcw.net/c/180381.html

2.C#Winform使用扩展方法自定义富文本框(RichTextBox)字体颜色

http://www.cnblogs.com/bobositlife/p/csharp-winform-change-richtextbox-font-color-using-static-extension-method.html

3.RichTextBox.SelectionLength 属性

https://msdn.microsoft.com/zh-cn/library/windows/desktop/system.windows.forms.richtextbox.selectionlength(v=vs.100).aspx/html

  用的时候都不是一帆风顺的,别人的:

  1.

private void AddTextAndSetFonts(RichTextBox rtx, string addStr, Color textColor)
{
int start = rtx.Text.Length;
int length = addStr.Length;
rtx.AppendText(addStr);
rtx.SelectionStart = start;
rtx.SelectionLength = length;
rtx.SelectionColor = textColor;
}
2.

public static void AppendTextColorful(this RichTextBox rtBox, string text, Color color, bool addNewLine = true)

    {
      if (addNewLine)
      {
        text += Environment.NewLine;
      }
      rtBox.SelectionStart = rtBox.TextLength;
      rtBox.SelectionLength = 0;
      rtBox.SelectionColor = color;
      rtBox.AppendText(text);
      rtBox.SelectionColor = rtBox.ForeColor;
    }
 
3

private void ModifySelectedText()
{
// Determine if text is selected in the control.
if (richTextBox1.SelectionLength > 0)
{
// Set the color of the selected text in the control.
richTextBox1.SelectionColor = Color.Red;
// Set the font of the selected text to bold and underlined.
richTextBox1.SelectionFont = new Font("Arial",10,FontStyle.Bold | FontStyle.Underline);
// Protect the selected text from modification.
richTextBox1.SelectionProtected = true;
}
}

 尤其要注意代码顺序不然会有问题,还有就是设置后要取消。经过自己改造后的:

private void AppendTextUnderline(RichTextBoxEx rtBox, string text, Color color, bool addNewLine)
{
if (addNewLine)
{
text += Environment.NewLine;
}
// int start = rtBox.Text.Length;
// int text_length = text.Length;

rtBox.SelectionStart = rtBox.Text.Length;
//rtBox.SelectionLength = text_length;
rtBox.SelectionColor = color;
rtBox.SelectionFont = new Font("Arial", 10, FontStyle.Bold | FontStyle.Underline);
rtBox.AppendText(text);
rtBox.SelectionColor = rtBox.ForeColor;
rtBox.SelectionFont = rtBox.Font;//恢复默认字体,否则会影响到后面
}

 

 

关于richtextbox改变字体颜色,加下划线的更多相关文章

  1. UILabel 字体下方加下划线

        UILabel *knowKankan = [[UILabel alloc] initWithFrame:CGRectMake(0, MAINHEIGHT - 78 , MAINWIDTH, ...

  2. 色彩缤纷的python(改变字体颜色及样式不完全版)

    色彩缤纷的python(改变字体颜色及样式) *补上昨天随笔中提到的改变字体颜色样式的方法,昨日随笔https://www.cnblogs.com/Du704/p/11265958.html 在项目过 ...

  3. 色彩缤纷的Python(改变字体颜色及样式)

    色彩缤纷的python(改变字体颜色及样式) 在项目过程中,我们常常会因为输出信息的颜色与样式过于单调以至于让人在视觉上感到很杂乱,所以看下文: 在Linux终端中,使用转义序列来进行如上所述的显示, ...

  4. poi生成excel整理(设置边框/字体/颜色/加粗/居中/)

    转: poi生成excel整理(设置边框/字体/颜色/加粗/居中/) 2016年12月02日 11:05:23 吃奶的牛 阅读数:34324   HSSFWorkbook wb = new HSSFW ...

  5. iOS初学,关于变量加下划线问题

    为什么做ios开发,变量前要加下划线才有用? 看到这个哥们的解释后,终于明白了,转帖到此. 链接在此:http://www.cocoachina.com/bbs/read.php?tid=234290 ...

  6. markdown改变字体颜色和大小

    markdown中改变字体颜色与大小方法同html 先看例子 <font face="黑体">我是黑体字</font> 我是黑体字 <font fac ...

  7. Markdown - 如何给文本加下划线

    解决方法 Markdown可以和HTML的语法兼容,可以通过HTML的标签来实现效果: 写法 效果 <u>下划线</u> 下划线 这里解释下,u指的是underline下划线. ...

  8. Android 改变字体颜色的三种方法

    在TextView中添加文本时有时需要改变一些文本字体的颜色,今天学到了三种方法,拿出来分享一下     1.在layout文件下的配置xml文件中直接设置字体颜色,通过添加android:textc ...

  9. echo改变字体颜色

    格式: echo -e "\033[字背景颜色;字体颜色m字符串\033[0m" 例如: echo -e "\033[41;36m something here \033 ...

随机推荐

  1. Docker 以 docker 方式运行 jenkins

    https://testerhome.com/topics/5798 Docker 以 docker 方式运行 jenkins jmcn · 2016年08月26日 · 最后由 blueshark 回 ...

  2. Spring Boot 2.0 整合Thymeleaf 模板引擎

    本节将和大家一起实战Spring Boot 2.0 和thymeleaf 模板引擎 1. 创建项目 2. 使用Spring Initlizr 快速创建Spring Boot 应用程序 3. 填写项目配 ...

  3. Socket网络编程--聊天程序(4)

    上一小节讲到可以实现多客户端与服务器进行通讯,对于每一个客户端的连接请求,服务器都要分配一个进程进行处理.对于多用户连接时,服务器会受不了的,而且还很消耗资源.据说有个select函数可以用,好像还很 ...

  4. 使用tar解压文件提示gzip: stdin: not in gzip format错误

    使用tar解压文件提示gzip: stdin: not in gzip format错误 1. 问题描述 使用docker save xxxx > xxx.tar导出镜像,由于文件太大,需要sp ...

  5. js判断登陆用户名及密码是否为空的简单实例

    js判断登陆用户名及密码是否为空的简单实例 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <script type="text/javascript ...

  6. 【emWin】例程三十一:窗口对象——Multipage

    简介: ULTIPAGE 类似于笔记本中的分隔卡或文件柜中的标签.通过使用MULTIPAGE 小工具,应用程序可为窗口或对话框的相同区域定义多个页面.每个页面包含特定 类型的信息或用户选择相应页面时应 ...

  7. 【Unity笔记】打包安卓APK时Build Setting中的三种Build System

    Internal(Default):Unity内置,仅需Android SDK支持.不能导出工程,适用于仅适用Unity开发的工程. Gradle(New):使用Gradle进行构建,需要Androi ...

  8. CefSharp 封装自己的简单H5浏览器 详细配置

    下载了最新的CefSharp源码,发现在自己机器可以运行,到普通的WIN7系统 报了  System.IO.FileNotFoundException: Could not load file or  ...

  9. 解决highCharts导出功能汉化问题

    本文以highCharts中文网上的例子为原型,处理解决highCharts导出功能为英文的问题. 我们使用highCharts当然希望所有提示或文本都是中文的了,但是highCharts的默认语言是 ...

  10. AES和RSA算法的demo代码

    aes代码示例: package com.autoyol.util.security.test; import java.security.Key; import java.security.NoSu ...