Extending ICSharpCode.TextEditor to support additional Syntax Highlighting

Need a great, simple to use free* syntax highlighting text editor for your custom app? Try the ICSharpCode.TextEditor used in the #develop project. (* I say free but obviously its bound by a license, LGPL currently...)

The ICSharpCode.TextEditor supporting SQL Syntax Highlighting
The ICSharpCode.TextEditor supporting SQL Syntax Highlighting

By default the ICSharpCode.TextEditor control supports mainly "code" oriented syntax highlighting. Extending it is actually fairly straight forward. The following steps are how I modified the control to include some basic SQL highlighting.

In the "Resources" directory, open "SyntaxModes.xml" and add something like:

<Mode file = "SQL-Mode.xshd"
   name = "SQL"
   extensions = ".sql"/>

Now, create a new XML file named "SQL-Mode.xshd" (or whatever is in your file attribute value).

** Note ** Make sure its an "Embedded Resource" via the build action for the file or you won't get any results.

See the "Mode.xsd" file for the schema definition but its pretty easy to follow from the other XSHD files. I just added a few keywords to get going:

<?xml version="1.0"?>
<SyntaxDefinition name = "SQL" extensions = ".sql">
  <
Digits name = "Digits" bold = "true" italic = "false" color = "Blue"/>
  <
RuleSets>
    <
RuleSet ignorecase = "true">
      <
Delimiters>&amp;&lt;&gt;~!%^*()-+=|\#/{}[]:;"' , .?</Delimiters>
      <
Span name = "String" bold = "false" italic = "false" color = "Blue" stopateol = "true">
        <
Begin>'</Begin>
        <
End>'</End>
      </
Span>
      <
KeyWords name = "OperatorKeywords" bold="true" italic="false" color="DarkCyan">
        <
Key word = "select" />
        <
Key word = "from" />
        <
Key word = "where" />
        <
Key word = "update" />
        <
Key word = "delete" />
        <
Key word = "set" />
        <
Key word = "as" />
      </
KeyWords>
    </
RuleSet>
  </
RuleSets>
</
SyntaxDefinition>

SQL-Mode.xshd Sample Source File

Recompile the project and drop the control into the Visual Studio .Net components tray. All you need now is to set the highlighting strategy. Somewhere in your initialization code, run the "SetHighlighting" method with the name of the new mode you defined, in this example "SQL":

textEditorControl1.SetHighlighting("SQL");

Easy! Now you can use the control in applications like the one below - a sample screenshot from my Mini SQL Query application:

Example screenshot from the Mini SQL Query application. >

This is the exact technique I have used in several 'in-house' tools, the latest being Mini SQL Query.

Last Update: May 2007.