Syntax Highlighting in Rich TextBox Control - Part 1

Introduction

This article shows how to perform basic syntax highlighting using the RichTextBox control and C#.The example shows how to apply highlighting as text is loaded into the RichTextBox.Another article Part 2 will show you how to do this interactively as the user enters in text.For this example, the language to be syntax highlighted within the RichTextBox control is C#.

Description

In this example, I take you through the following steps:

  • the creation of a RichTextBox control, 
  • the definition the input language C# to be highlighted, 
  • the splitting of the input language into individual lines, and 
  • then the processing the text within each line and how the RichTextBox control properties are
  • used to set the individual text colors and fonts. 

Lets begin with creating a normal RichTextBox control like so.

Note, I define the RichTextBox control as a class variable so I can access it from other methods within the same class. This isnt shown here.

  1. RichTextBox m_rtb = null;  
  2. m_rtb = new RichTextBox();  
  3. m_rtb.Multiline = true;  
  4. m_rtb.WordWrap = false;  
  5. m_rtb.AcceptsTab = true;  
  6. m_rtb.ScrollBars = RichTextBoxScrollBars.ForcedBoth;  
  7. m_rtb.Dock = DockStyle.Fill;  
  8. m_rtb.SelectionFont = new Font("Courier New", 10,FontStyle.Regular);  
  9. m_rtb.SelectionColor = Color.Black;  
The language I will highlight is C# and I have stored it as a string, but this could be read from a file or from wherever.
  1. String inputLanguage =   
  2. "// Comment.\n" +  
  3. "using System;\n" +   
  4. "\n" +  
  5. "public class Stuff : Form { \n" +  
  6. " public static void Main(String args) {\n" +  
  7. " }\n" +   
  8. "}\n" ;   
Then we can use the Regex class to split the input language into individual lines like so.
  1. Regex r = new Regex(\\n);  
  2. String [] lines = r.Split(inputLanguage);  
Next, I take each line and process its contents like so.
  1. foreach (string l in lines)  
  2. {  
  3.     ParseLine(l);  
  4. }  
Now, this is the interesting part where we perform syntax highlighting within my method ParseLine(). The method begins by defining a few keywords that will be given special highlighting when they are detected. Note, you could extend this and use a lookup table with keywords and their corresponding font and color.

Next the contents of the input line are split into multiple tokens using the Regex class. The group syntax () is used to pull out the tokens including the delimiters. Then each token is examined to see whether it is a keyword or not. Before the text is added to the RichTextBox we must set the color and font of the text using the RichTextBox properties SelectionColor and SelectionFont. After this we can simply add the text tokens to the RichTextBox using the property SelectedText which acts like an append text function. The text tokens will now be displayed in the color and font we select.

Note, I have removed the code to detect comments // since it makes the code look cleaner. See the attached file for a more complete example.

  1. void ParseLine(string line)   
  2. {  
  3.     Regex r =new Regex("([ \\t{}():;])");  
  4.     String [] tokens = r.Split(line);   
  5.     foreach (string token in tokens)   
  6.     {   
  7.         // Set the tokens default color and font.  
  8.         m_rtb.SelectionColor = Color.Black;  
  9.         m_rtb.SelectionFont =new Font("Courier New", 10, FontStyle.Regular);   
  10.          // Check whether the token is a keyword.   
  11.         String [] keywords = {"public""void""using""static""class"};  
  12.         for (int i = 0; i < keywords.Length; i++)  
  13.         {  
  14.             if (keywords[i] == token)  
  15.             {  
  16.                 // Apply alternative color and font to highlight keyword.  
  17.                 m_rtb.SelectionColor = Color.Blue;  
  18.                 m_rtb.SelectionFont =new Font("Courier New", 10,FontStyle.Bold);  
  19.                 break;  
  20.             }  
  21.         }  
  22.         m_rtb.SelectedText = token;  
  23.     }  
  24.     m_rtb.SelectedText = "\n";  
  25. }   
Below is a screen grab of the RichTextBox highlighting application in operation.

Conclusion

This article described a rudimentary example of how to perform syntax highlighting as text is loaded into the RichTextBox control. A more thorough implementation is left to the reader. A second article will describe how to perform syntax highlighting interactively as the user enters in text.