C# – RichTextBox Syntax Highlighting

26 11 2009

There’s a few different methods to make your application apply syntax highlighting, this method below works for me, and although there’s many better, and more efficient ways it can be done, this is simple. Really simple.

First off, you need a reference for RegularExpressions –

using System.Text.RegularExpressions;

…and you’ll need to create a Rich Text Box, and either a button or menu item – you can have this working on a timer or a ‘TextChanged’ function, but it requires a bit more work, and can get very messy with large sections of text.

We need to declare regular expression strings to a variable that we’ll be searching for, it’s easiest to place it in the ‘global declaration’ area.. So right after:

public partial class Form1 : Form
{

Create your variable declaration:


public Regex keyWordsBlue = new Regex("if |then |else |fi |true |while |do |done |set |export |bool |break |case |class |const |for |foreach |goto |in |void |if\n|then\n|else\n|fi\n|true\n|while\n|do\n|done\n|set\n|export\n|bool\n|break\n|case\n|class\n|const\n|for\n|foreach\n|goto\n|in\n|void\n");

This is basically saying anything between the quotes, delimited by a pipe(|) symbol will get highlighted.

I’ve picked some common protected function words in Unix for my example. Since I don’t want *every* occurance of this to be highlighted, i’ve made it so the word requires a space or a return (new line \n) character after it, which 99% of the time it will have.

The next bit will apply the highlighting. Personally I prefer to have this in a button, or menu item – if you want this to run on ‘TextChanged’ or via a timer, you’ll need to edit it ever so slightly.

Remember to create this in a seperate function, so you can call it from different places as/when required.

        private void ApplySyntaxHighlighting()
        {

            // Select all and set to black so that it's 'clean'
            rtbText.SelectAll();
            rtbText.SelectionColor = Color.Black;

            // Then unselect and scroll to the end of the file
            rtbText.ScrollToCaret();
            rtbText.Select(rtbText.Text.Length, 1);

            // Start applying the highlighting... Set a value to selPos
            int selPos = rtbText.SelectionStart;

            foreach (Match keyWordMatch in keyWordsBlue.Matches(rtbText.Text))
            {
                // Select the word..
                rtbText.Select(keyWordMatch.Index, keyWordMatch.Length);
                // Change it to blue
                rtbText.SelectionColor = Color.Blue;
                // Set it to bold for this example
                rtbText.SelectionFont = new Font(rtbText.SelectionFont, FontStyle.Bold);
                // Move cursor back to where it was
                rtbText.SelectionStart = selPos;
                // Change the default font color back to black.
                rtbText.SelectionColor = Color.Black;
            }
        }

Now, in your button or menu item, you need to call the function with “ApplySyntaxHighlighting();” – i.e.


private void applySyntaxHighlightingToolStripMenuItem_Click(object sender, EventArgs e)
{
ApplySyntaxHighlighting();
}

Once you’ve added some different regular expression strings, it should start looking a bit like this:

Example

P.S – The expression I used for the comment line was:


public Regex keyWordsGreenComment = new Regex(@"\#.*?\n");


Actions

Information

Leave a comment