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.

RichTextBox m_rtb = null;
m_rtb =
new RichTextBox();
m_rtb.Multiline =
true;
m_rtb.WordWrap =
false;
m_rtb.AcceptsTab =
true;
m_rtb.ScrollBars = RichTextBoxScrollBars.ForcedBoth;
m_rtb.Dock = DockStyle.Fill;
m_rtb.SelectionFont =
new Font("Courier New", 10,FontStyle.Regular);
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.

String inputLanguage =
"// Comment.\n" +
"using System;\n" +
"\n" +
"public class Stuff : Form { \n" +
" public static void Main(String args) {\n" +
" }\n" +
"}\n" ; 

Then we can use the Regex class to split the input language into individual lines like so.

Regex r = new Regex(\\n);
String [] lines = r.Split(inputLanguage);
Next, I take each line and process its contents like so.
foreach (string l in lines)
{
ParseLine(l);
}

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.

void ParseLine(string line) {
Regex r =
new Regex("([ \\t{}():;])");
String [] tokens = r.Split(line);
foreach (string token in tokens) {
// Set the tokens default color and font.
m_rtb.SelectionColor = Color.Black;
m_rtb.SelectionFont =
new Font("Courier New", 10, FontStyle.Regular);
// Check whether the token is a keyword.
String [] keywords = {"public", "void", "using", "static", "class"};
for (int i = 0; i < keywords.Length; i++) {
if (keywords[i] == token) {
// Apply alternative color and font to highlight keyword.
m_rtb.SelectionColor = Color.Blue;
m_rtb.SelectionFont =
new Font("Courier New", 10,FontStyle.Bold);
break;
}
}
m_rtb.SelectedText = token;
}
m_rtb.SelectedText = "\n";
}

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.

Up Next
    Ebook Download
    View all
    Learn
    View all