RichTextbox 着色行为 [英] RichTextbox coloring behaviour
问题描述
我目前正在尝试开发一个行为类似于 Notepad++s 的软件.关于着色"部分,我使用正则表达式和包含正则表达式的外部文件 &每个单词的颜色.
I am currently trying to develop an software whose behaviour is similar to Notepad++s. Regarding the 'coloring' part, I use regex and an external file containing the regex & color of each word.
文件如下:
<script&blue
/>&blue
\".*?\"&red
然后软件读取文件并通过在每个换行符处将其拆分来将其转换为字符串数组string[]".此数组称为校正器".然后我使用以下方法来查找 &设置匹配正则表达式模式的每个单词的颜色:
The software then reads the file and converts it into an string array 'string[]' by splitting it at each newline character. This array is called 'Correctors'. I then use following method to find & set the color of each word matching regex pattern:
foreach (string corrector in Correctors) {
string[] spTxt = corrector.Split('&');
Match matches = Regex.Match(rtb_Main.Text, spTxt[0]);
Color color = Color.FromName(spTxt[1]);
while (matches.Success)
{
rtb_Main.SelectionStart = matches.Index;
rtb_Main.SelectionLength = matches.Length;
rtb_Main.SelectionColor = color;
matches = matches.NextMatch();
}
}
这就是问题发生的地方.该方法按预期用于数组Correctors"中的最后一个字符串.然而;似乎数组中的其他对象要么被覆盖,要么被忽略,因为与其模式匹配的单词没有被着色.
This is where the issue occurs. The method is working as supposed for the last string in the array 'Correctors'. However; it seems that the other objects in the array is being either overwritten or ignored as the words matching their patterns is not being colored.
怎么了?
提前致谢,
- 拉斯穆斯.
Thanks in advance,
- Rasmus.
推荐答案
您在编译时没有遇到任何错误.我的意思是说你是怎么得到的:
Didn't you get any error while compiling this. I mean to say how did you get:
while (matches.Success)
应该是这样..
// Use foreach loop.
foreach (Match match in matches)
{
if(match.Success)
{
//Change color here...
}
}
这篇关于RichTextbox 着色行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!