如何通过RTF逐字更改颜色? [英] How to change color by word in rich text ?

查看:347
本文介绍了如何通过RTF逐字更改颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过变量中的值更改字体颜色.如果富文本为"hi",则单词"hi"的颜色将转换为红色.如果是我",则单词您"的颜色为蓝色.换句话说,字体颜色是黑色.与C#相同,以查找单词``if''->``if''的颜色为蓝色.

以下代码非常有效:

I want to change the font color by value from the variable. If rich text is "hi", the color of the word "hi" is converted to red. If it is "me", the color of the word "you" is blue. In other words, the font color is black. Same as C# to find the word ''if''->''if'' color is blue.

The following code works very well:

leng = richTextBox1.Text.Length;
txtL.Text = leng.ToString();
if (leng > 2)
{

     leng = leng - 2;
     //Filter 2 char.
     textBox1.Text = richTextBox1.Text.Substring(leng, 2);
     fchar = richTextBox1.Text.Substring(leng, 2);
     leng = richTextBox1.Text.Length;
}
if (fchar == "hi")
{
     richTextBox1.SelectionStart = richTextBox1.Text.Length - 2;
     richTextBox1.SelectionLength = 2;
     richTextBox1.SelectionColor = Color.Red;
     richTextBox1.SelectionStart = richTextBox1.Text.Length;
     richTextBox1.SelectionLength = 1;
     richTextBox1.SelectionColor = Color.Black;
}
if (fchar == "me")
{
     richTextBox1.SelectionStart = richTextBox1.Text.Length - 2;
     richTextBox1.SelectionLength = 2;
     richTextBox1.SelectionColor = Color.Blue;
     richTextBox1.SelectionStart = richTextBox1.Text.Length;
     richTextBox1.SelectionLength = 1;
     richTextBox1.SelectionColor = Color.Black;
}


但是有一个问题是,当我在后面或前面的喜字中添加字符时.
例如:hi-> hii
文字颜色为红色
我想在单词hi->的开头或结尾添加字符.将会有黑色,并且字符添加为黑色


But there is a problem is when I add characters to the back or front hi words.
for example: hi-> hii
color of the text is red
I want to add characters at the beginning or end of words hi-> hi there will be black and the characters are added in black
how to do this?

推荐答案

您可以看到此链接
单击
You can see this link
Click


谢谢.您的解决方案.我非常感激
我找到了正则表达式,它解决了我的问题.代码如下:

thanks.your solution.I am very grateful
I found Regex.It solved my problem.Here''s the code:

  //found keyword 'if' or 'while' or '


' for (匹配match4 = 正则表达式(" 如果|同时|
' for(Match match4 = new Regex("if | while |


这篇关于如何通过RTF逐字更改颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆