7
Answers

HTML Editor Character color help

David Pullman

David Pullman

15y
2.5k
1
Hey guys, I'm making an HTML editor.  I want to be able to have the HTML tags show up as a different color than the rest of the text.  For example:

<p>Hello C#!</p>

I want the paragraph tags to be highlighted as a different color as the text (which is black by default).

I was thinking of something like this.

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            string text = richTextBox1.Text;
            char[] sep = { ' ', '\n', '.', ',', ':', ';' };
            string[] words = text.Split(sep, System.StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < text.Length; i++)
            {
                if (text[i] == '<')
                {
                    //change color of tag here
                }
            }
        }

Thats just a rough idea.  I know I'm gonna have to add code that will loop through the text and when it finds the '<' loop through the characters there and then stop the color change with the '>' character is found.  I know it needs a lot of work, but I'm kinda new at this.  Any suggestions or help.  If I can get this to work I would be so happy.  I've been messing with it for a while.  Please let me know.

-Dave
Answers (7)