Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
228 views
Welcome To Ask or Share your Answers For Others

1 Answer

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...
                }
            }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...