Reader Level:

Syntax Highlighting in RichTextBox Control - Part 2

By Duncan Harris on Jun 12, 2003
This article shows how to perform interactive syntax highlighting within the RichTextBox control.
  • 0
  • 0
  • 85592
Download Files:


This article shows how to perform interactive syntax highlighting within the RichTextBox control. The source code is written in C#.

This text builds upon the previous article Syntax Highlighting within the RichTextBox Control - Part 1 that described how to highlight text as it is loaded into the RichTextBox.


To perform interactive syntax highlighting within the RichTextBox, the events generated by the control need to be monitored. In this example I monitor the event TextChanged which is triggered each time text is modified within the control. Further to make life easy apply formatting to the entire line that the user is editing. Note, this implmentation has some drawbacks in terms of screen flicker. More about that later.

We begin by attaching an event handler to the RichTextBox property TextChanged like so.

RichTextBox m_rtb = null;
m_rtb =
new RichTextBox();
m_rtb.TextChanged +=
new EventHandler(this.TextChangedEvent)

Next I define the TextChanged() method to handle the event and perform interactive text highlighting to the entire line being edited by the user. The method begins by locating the start and end offset for the current line being edited. Next I extract this line of text from the RichTextBox so it can be processed. Before processing the line of text, I must save the RichTextBox properties SelectionStart and SelectionLength since it is these properties that are used to set the individual text highlighting positions. Next I split the line of text into tokens and analyze each token to see whether it is a keyword that requires special highlighting or not and then apply the font and color. Finally, I return the original values of the RichTextBox properties SelectionStart and SelectionLength.

Note, I have removed the highlighting code for comments to make the example more readable. See the attached file for a more complete example.

private void TextChangedEvent(object sender, EventArgs e) {
// Calculate the starting position of the current line.
int start = 0, end = 0;
for (start = m_rtb.SelectionStart - 1; start > 0; start--) {
if (m_rtb.Text[start] == '\n') { start++; break; }
// Calculate the end position of the current line.
for (end = m_rtb.SelectionStart; end < m_rtb.Text.Length; end++) {
if (m_rtb.Text[end] == '\n') break;
// Extract the current line that is being edited.
String line = m_rtb.Text.Substring(start, end - start);
// Backup the users current selection point.
int selectionStart = m_rtb.SelectionStart;
int selectionLength = m_rtb.SelectionLength;
// Split the line into tokens.
Regex r = new Regex("([ \\t{}();])");
string [] tokens = r.Split(line);
int index = start;
foreach (string token in tokens) {
// Set the token's default color and font.
m_rtb.SelectionStart = index;
m_rtb.SelectionLength = token.Length;
m_rtb.SelectionColor = Color.Black;
m_rtb.SelectionFont =
new Font("Courier New", 10,
// 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,

index += token.Length;
// Restore the users current selection point.
m_rtb.SelectionStart = selectionStart;
m_rtb.SelectionLength = selectionLength;

Note, updating a whole line of text produces some noticeable flicker on the line of text being entered by the user. A more thorough implementation of highlighting might use a data structure to store the formatting properties of all text characters within the control so that only necessary updates are performed in the control. Moreover, the KeyPressed event could be utilized to handle the insertion of individual characters into the control.


This article described a rudimentary example of how to perform syntax highlighting interactively within the RichTextBox control. A more thorough implementation is left to the reader.

Article Extensions
Contents added by chung tran on Jul 28, 2012
great source

Trending up