Click here to Skip to main content
15,886,632 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
I have some code that should color some text in a richtextbox, currently what it should do, is character by character color every word in the richtextbox, but it leaves some characters still black at the end for some reason.

This is the timer1 tick method
C#
private void timer1_Tick(object sender, EventArgs e)
        {
            foreach (RichTextBox rtb in Editor.Values)
            {
                int pos = 0;
                foreach(string ln in rtb.Lines)
                {
                    Color[] Syntax = EditorTools.ParseString(ln);
                    int cur = 0;
                    foreach (char chr in ln)
                    {
                        rtb.SelectionStart = pos;
                        rtb.SelectionLength = 1;
                        rtb.SelectionColor = Syntax[cur];
                        cur++;
                        pos++;
                    }
                }
            }
        }


And this is the ParseString method which returns a Color array
C#
public static Color[]ParseString(string raw)
        {
            int len = 0;
            foreach(char chr in raw)
            {
                len++;
            }
            Color[] ret = new Color[len];
            string[] parsed = Regex.Split(raw, " ");
            for (int i = 0;i < len;i++)
            {
                ret[i] = Color.Green;
            }
            return ret;
        }
Posted
Comments
ridoy 26-Aug-13 14:32pm    
what you actually want to do?
Sicppy 26-Aug-13 14:33pm    
Eventually it will evolve into a syntax highlighter, but for now, its supposed to just color everything green, i want to do it char by char to test the syntax application method

1 solution

 
Share this answer
 
Comments
Sicppy 26-Aug-13 15:45pm    
Thank you for the resource!
ridoy 26-Aug-13 16:01pm    
glad to help you.
BillWoodruff 27-Aug-13 4:14am    
excellent answer !
ridoy 27-Aug-13 7:06am    
Thanks Bill, :)
CharlesMitchell 19-Mar-18 7:38am    
I am looking for WPF VB version. Any Ideas
Thanks Charles

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900