RichTextbox coloring behaviour

…衆ロ難τιáo~ 提交于 2019-12-13 03:36:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!