问题
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.
The file looks following:
<script&blue
/>&blue
\".*?\"&red
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();
}
}
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.
What is wrong?
Thanks in advance,
- Rasmus.
回答1:
Didn't you get any error while compiling this. I mean to say how did you get:
while (matches.Success)
It should have been like this..
// Use foreach loop.
foreach (Match match in matches)
{
if(match.Success)
{
//Change color here...
}
}
来源:https://stackoverflow.com/questions/12437880/richtextbox-coloring-behaviour