参考了三份有用的资料:

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. [转]Greenplum 执行计划之广播与重分布

    关联数据在不同节点上,对于普通关系型数据库来说,是无法进行连接的.关联的数据需要通过网络流入到一个节点中进行计算,这样就需要发生数据迁移.数据迁移有广播和重分布两种.在GP中,每一个广播或重分布会产生 ...

  2. echarts 通过dom获取echarts实例,批量监听reset

    重点在于 echarts.getInstanceByDom 这个 API 我的js: // 懒加载优化:滚动节流策略 var __SCROLLTIMER__ = null // 重新设置 echart ...

  3. Nuget 配置文件的位置

    最近在 Visual Studio 中使用 Nuget 时,发现总是连接代理服务器,忘了什么时候配置的了,找了半天没找到配置位置,最后发现在这个地方: %appdata%\NuGet 找到 NuGet ...

  4. java 路径分隔符自动适配

    linux文件路径分隔符为 /  ,windows的文件路径分隔符为  \   ,在开发项目过程中不确定用户使用何种操作系统,就需要自动适配路径. 目前已知java提供两种方法获取文件路径分割符: F ...

  5. Intent的作用和表现形式简单介绍

    Intent的作用和表现形式简单介绍 1.描写叙述:Intent负责相应用中一次操作的动作,动作涉及的数据,附加数据进行描写叙述.系统或者应用依据此Intent的描写叙述,负责找到相应的组件,将Int ...

  6. 30. Substring with Concatenation of All Words

    题目: You are given a string, s, and a list of words, words, that are all of the same length. Find all ...

  7. python安装模块

    pychram安装模块,非常简单!

  8. centos6.5安装mongodb2.6

    下载地址:http://www.mongodb.org/downloads 解压命令:tar zxf mongodb-linux-i686-2.6.0.tgz 存放目录:/usr/local/mong ...

  9. Python爬虫学习——使用selenium和phantomjs爬取js动态加载的网页

    1.安装selenium pip install selenium Collecting selenium Downloading selenium-3.4.1-py2.py3-none-any.wh ...

  10. swoole 定时器

    timer.php <?php //创建websocket服务器对象,监听0.0.0.0:9502端口 $ws = ); swoole_timer_tick(, function ($timer ...