Click here to Skip to main content
13,291,254 members (54,897 online)
Click here to Skip to main content
Add your own
alternative version


234 bookmarked
Posted 14 Jun 2005

Enabling syntax highlighting in a RichTextBox

, 14 Jun 2005
Rate this:
Please Sign up or sign in to vote.
An article on how to enable syntax highlighting in a RichTextBox.

Sample Image - SyntaxRichTextBox.gif


I am currently involved in writing a game called Hydria that uses Lua as a scripting language. To handle the scripts for different npc:s, I got the assignment of writing a simple editor that would be implemented in the npc-editor. Everything worked fine but editing scripts can be a real pain if there is no syntax highlighting. So I decided to write a control that could do that.

I started my research on the subject and found several good articles. One of the most useful articles I found was one by Michael Gold and my work is in some parts based on his.

This control is meant to be used for simple syntax highlighting, so it is far from perfect. For instance, you can not (yet) define C-style comments like <CODE lang=c++>/* comment */ but I'm going to implement it in the next version.

Since I'm using generics, this code only works with version >= 2.0 of the .NET Framework.

The control

The <CODE lang=c++>SyntaxRichTextBox is (as the name reveals) derived from <CODE lang=c++>RichTextBox but has some slight differences.


  • List<string> Settings.KeyWords

    The keywords that will be used in the SyntaxRichTextBox.

  • Color Settings.KeywordColor

    The color that will be used to colorize the keywords (default blue).

  • string Settings.Comment

    The identifier for comments. As I mentioned in the introduction, C-style comments like <CODE lang=c++>/* comment */ is not yet implemented.

  • Color Settings.CommentColor

    The color that will be used to colorize comments (default green).

  • Color Settings.StringColor

    The color that will be used to colorize strings (default grey).

  • Color Settings.IntegerColor

    The color that will be used to colorize integers (default red).

  • bool Settings.EnableComments

    If this property is set to false, comments will not be processed.

  • bool Settings.EnableStrings

    If this property is set to false, strings will not be processed.

  • bool Settings.EnableIntegers
  • If this property is set to false, integers will not be processed.


  • public void CompileKeywords()

    Compiles the keywords into a regular expression. This function should be called when you've added all keywords.

  • public void ProcessAllLines()

    Updates the syntax highlighting of all text in the SyntaxRichTextBox control.

Using the SyntaxRichTextBox control

Step 1:

You can either add the whole SyntaxHighlighter project to your solution by right-clicking the root in the solution explorer and choose "Add -> Existing Project" or you can add the assembly to the toolbox by right-clicking on the toolbox view and select "Choose Items...".

Step 2:

Create the control from the toolbox by dragging SyntaxRichTextBox to the form. If you chose to add the SyntaxHighlighter project to your solution, SyntaxRichTextBox is located in a new tab in the toolbox-view that's called "SyntaxHighlighter components". If you chose to add the assembly to the toolbox directly, SyntaxRichTextBox will be located in the tab "Common Controls".

Step 3:

Now right click on the newly created SyntaxRichTextBox and select "Properties...". Give the control a suitable name. In this example, I give the control the name m_syntaxRichTextBox.

Step 4:

Now we have added the control to our form and now we should prepare it with some keywords. Let's make a simple highlighter for the script language Lua as an example.

private void MainForm_Load(object sender, EventArgs e)
    // Add the keywords to the list.

    // Set the comment identifier. 
    // For Lua this is two minus-signs after each other (--).
    // For C++ code we would set this property to "//".
    m_syntaxRichTextBox.Settings.Comment = "--";

    // Set the colors that will be used.
    m_syntaxRichTextBox.Settings.KeywordColor = Color.Blue;
    m_syntaxRichTextBox.Settings.CommentColor = Color.Green;
    m_syntaxRichTextBox.Settings.StringColor = Color.Gray;
    m_syntaxRichTextBox.Settings.IntegerColor = Color.Red;

    // Let's not process strings and integers.
    m_syntaxRichTextBox.Settings.EnableStrings = false;
    m_syntaxRichTextBox.Settings.EnableIntegers = false;

    // Let's make the settings we just set valid by compiling
    // the keywords to a regular expression.

    // Load a file and update the syntax highlighting.
    m_syntaxRichTextBox.LoadFile("script.txt", RichTextBoxStreamType.PlainText);

Known issues

  • Colorizing fails to work when pasting something larger than one line.
  • C-style comments not yet implemented.
  • Can be slow sometimes. Optimization needed.

Points of Interest

This is my first C# article here on CodeProject. I have only worked with C# for about three months so it is probably very much that I'm not aware of. If you see something in the code that makes you laugh :) or something that is just plain wrong, tell me so and I will correct it.

The control is not in any way perfect but my plan is to continue working on it until I'm totally satisfied.



  • 2005-06-14

    First revision of this article posted.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Patrik Svensson
Software Developer
Sweden Sweden
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionCan't change default color of text. Pin
blackstormx11-Mar-17 23:48
memberblackstormx11-Mar-17 23:48 
Question做的很棒 Pin
vaemc28-Jan-17 17:22
membervaemc28-Jan-17 17:22 
QuestionHow to arrange the text automatically? Pin
Christopher_liang16-Dec-16 16:21
memberChristopher_liang16-Dec-16 16:21 
AnswerUserful Pin
Christopher_liang16-Dec-16 15:31
memberChristopher_liang16-Dec-16 15:31 
QuestionQuestion about the comments Pin
eladioalberto.0218-Mar-16 20:25
membereladioalberto.0218-Mar-16 20:25 
QuestionBoth the source code and the demo do not work Pin
Member 83787699-Jul-15 5:57
memberMember 83787699-Jul-15 5:57 
QuestionHow to add HTML tags? Pin
Jakubek070324-Jun-15 7:44
memberJakubek070324-Jun-15 7:44 
QuestionSyntax of RTF code Pin
Member 835089828-Feb-15 23:50
memberMember 835089828-Feb-15 23:50 
QuestionErrors when opening project in Visual Studio Pin
Member 1052568811-Feb-15 4:02
memberMember 1052568811-Feb-15 4:02 
QuestionI need to start one of my keywords using "!" character Pin
almendietamor9-Apr-14 19:56
memberalmendietamor9-Apr-14 19:56 
QuestionGreat! Save me a lot of time, thank you Pin
almendietamor9-Apr-14 17:41
memberalmendietamor9-Apr-14 17:41 
QuestionMulti-line comment Pin
Wrangly6-Nov-13 6:17
memberWrangly6-Nov-13 6:17 
GeneralNew Editor Features :) Pin
AIbanezW19-Sep-13 14:34
memberAIbanezW19-Sep-13 14:34 
BugIt looks like the undo (ctrl+z) is broken for this control Pin
Ilan Firsov13-Jan-13 23:47
memberIlan Firsov13-Jan-13 23:47 
GeneralThanks Pin
kevininstructor31-Dec-12 16:38
memberkevininstructor31-Dec-12 16:38 
Questionpasting multiple lines and only the last line is processed Pin
wxhubo26-Sep-12 17:30
memberwxhubo26-Sep-12 17:30 
AnswerRe: pasting multiple lines and only the last line is processed Pin
Member 1292947931-Dec-16 11:59
memberMember 1292947931-Dec-16 11:59 
SuggestionSpend a lot of time Pin
jasonangela7-Aug-12 15:44
memberjasonangela7-Aug-12 15:44 
GeneralRe: Spend a lot of time Pin
Member 83787699-Jul-15 6:29
memberMember 83787699-Jul-15 6:29 
GeneralMy vote of 4 Pin
Burak Tunçbilek25-Jul-12 5:52
memberBurak Tunçbilek25-Jul-12 5:52 
GeneralMy vote of 5 Pin
RafalChmiel11-Apr-12 9:41
memberRafalChmiel11-Apr-12 9:41 
Bugout of range display Pin
dvir12320-Mar-12 5:33
memberdvir12320-Mar-12 5:33 
QuestionGood code Pin
jlkdaslkfjd6-Aug-11 17:07
memberjlkdaslkfjd6-Aug-11 17:07 
GeneralMy vote of 1 Pin
anoop_dp25-Jul-11 1:38
memberanoop_dp25-Jul-11 1:38 
GeneralRe: My vote of 1 Pin
jb060820-Sep-11 1:52
memberjb060820-Sep-11 1:52 
idiot - at the very least explain then problems you encounter
GeneralRe: My vote of 1 Pin
RafalChmiel12-Apr-12 2:30
memberRafalChmiel12-Apr-12 2:30 
GeneralRe: My vote of 1 Pin
DelphiCoder29-Apr-14 13:59
memberDelphiCoder29-Apr-14 13:59 
GeneralThank you Pin
C#Wolf26-Feb-11 7:25
memberC#Wolf26-Feb-11 7:25 
QuestionHow about putting the settings in the class itself? Pin
jsoldi2-Oct-10 17:32
memberjsoldi2-Oct-10 17:32 
GeneralProperties Settings.... Pin
tim_mcgwyn17-Apr-10 11:53
membertim_mcgwyn17-Apr-10 11:53 
GeneralRe: Properties Settings.... Pin
jsoldi2-Oct-10 17:36
memberjsoldi2-Oct-10 17:36 
GeneralMy vote of 2 Pin
AlexanderBorup19-Feb-10 1:29
memberAlexanderBorup19-Feb-10 1:29 
Generalperformance problems Pin
f.developpement16-Nov-09 4:25
memberf.developpement16-Nov-09 4:25 
GeneralRe: performance problems Pin
wuhi18-Nov-09 5:28
memberwuhi18-Nov-09 5:28 
Generalperformance problemes solved Pin
wuhi12-Nov-09 21:22
memberwuhi12-Nov-09 21:22 
GeneralRe: performance problemes solved Pin
leosco17-Dec-09 3:31
memberleosco17-Dec-09 3:31 
GeneralRe: performance problemes solved Pin
Joao Tito Livio27-Dec-09 14:00
memberJoao Tito Livio27-Dec-09 14:00 
GeneralRe: performance problemes solved Pin
b4326854@uggsrock.com22-Feb-10 19:27
memberb4326854@uggsrock.com22-Feb-10 19:27 
GeneralRe: performance problemes solved Pin
iRoN_RoCK22-Jul-10 5:54
memberiRoN_RoCK22-Jul-10 5:54 
GeneralRe: performance problemes solved Pin
Jonathan Nappee10-Dec-10 8:13
memberJonathan Nappee10-Dec-10 8:13 
QuestionHow to apply syntax coloring for SAS language? Pin
JulienV10-Nov-09 3:04
memberJulienV10-Nov-09 3:04 
GeneralThanks Pin
GlimmerMan14-Oct-09 11:16
memberGlimmerMan14-Oct-09 11:16 
GeneralRegex to slow :( [modified] Pin
no_morser22-Jul-09 0:16
memberno_morser22-Jul-09 0:16 
GeneralRe: Regex to slow :( Pin
michele_cv22-Jul-09 2:57
membermichele_cv22-Jul-09 2:57 
GeneralRe: Regex to slow :( Pin
no_morser24-Jul-09 2:37
memberno_morser24-Jul-09 2:37 
GeneralRe: Regex to slow :( Pin
tim_mcgwyn17-Apr-10 11:44
membertim_mcgwyn17-Apr-10 11:44 
Generalabout regex string Pin
Gumbon20-May-09 4:35
memberGumbon20-May-09 4:35 
Generalit is much better to add one more term for functions Pin
Seraph_summer7-May-09 10:55
memberSeraph_summer7-May-09 10:55 
QuestionTextChanged event not "reaching" a parent control Pin
efisir10-Dec-08 7:50
memberefisir10-Dec-08 7:50 
AnswerRe: TextChanged event not "reaching" a parent control Pin
Member 19325392-Feb-09 7:29
memberMember 19325392-Feb-09 7:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171207.1 | Last Updated 14 Jun 2005
Article Copyright 2005 by Patrik Svensson
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid