![]() |
Desktop Development »
Edit Controls »
General
Intermediate
Enabling syntax highlighting in a RichTextBoxBy Patrik SvenssonAn article on how to enable syntax highlighting in a RichTextBox. |
C#.NET 2.0, Win2K, WinXPVS2005, Dev
|
|
Advanced Search |
|
|
|
||||||||||||||||

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 /* 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 SyntaxRichTextBox is (as the name reveals) derived from 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 /* 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
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.
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...".
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".
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.
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.
m_syntaxRichTextBox.Settings.Keywords.Add("function");
m_syntaxRichTextBox.Settings.Keywords.Add("if");
m_syntaxRichTextBox.Settings.Keywords.Add("then");
m_syntaxRichTextBox.Settings.Keywords.Add("else");
m_syntaxRichTextBox.Settings.Keywords.Add("elseif");
m_syntaxRichTextBox.Settings.Keywords.Add("end");
// 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.
m_syntaxRichTextBox.CompileKeywords();
// Load a file and update the syntax highlighting.
m_syntaxRichTextBox.LoadFile("script.txt", RichTextBoxStreamType.PlainText);
m_syntaxRichTextBox.ProcessAllLines();
}
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.
First revision of this article posted.
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 14 Jun 2005 Editor: Sumalatha K.R. |
Copyright 2005 by Patrik Svensson Everything else Copyright © CodeProject, 1999-2009 Web17 | Advertise on the Code Project |