[Expand]General Information
[Collapse]WinForms Controls
 [Expand]What's Installed
 [Expand]Build an Application
 [Collapse]Controls and Libraries
  [Expand]Forms and User Controls
  [Expand]Messages, Notifications, and Dialogs
  [Expand]Editors and Simple Controls
  [Expand]Ribbon, Bars and Menu
  [Expand]Application UI Manager
  [Expand]Docking Library
  [Expand]Data Grid
  [Expand]Vertical Grid
  [Expand]Property Grid
  [Expand]Pivot Grid
  [Expand]Tree List
  [Expand]Chart Control
  [Expand]Map Control
  [Collapse]Rich Text Editor
   [Expand]Product Information
    Product Class Structure
    Supported Formats
   [Expand]Getting Started
   [Expand]RichEditControl Document
    Text Formatting
    Import and Export
   [Expand]Page Layout
    Spell Checking
    AutoCorrect Feature
    Mail Merge
    Restrictions and Protection
   [Expand]Visual Elements
    Keyboard Shortcuts
    HTML Tag Support
    [Expand]Text Boxes
    [Expand]Document Elements
      How to: Create a Table of Contents
      How to: Create a Table of Contents in Code
      How to: Handle the HyperlinkClick Event to Invoke the Custom Form
      How to: Retrieve the List of Document Fonts using the Visitor-Iterator Pattern
      How to: Highlight Document Syntax
      How to: Insert Dynamic Content
    [Expand]Search and Replace
    [Expand]Spell Check
    [Expand]Import and Export
    [Expand]UI Customization
  [Expand]Spell Checker
  [Expand]Form Layout Managers
  [Expand]Navigation Controls
  [Expand]PDF Viewer
  [Expand]TreeMap Control
  [Expand]Sunburst Control
 [Expand]Common Features
  Get More Help
 [Expand]API Reference
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Expand]WPF Controls
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Office File API
[Expand]Report and Dashboard Server
[Expand]eXpressApp Framework
[Expand]eXpress Persistent Objects
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation
View this topic on docs.devexpress.com (Learn more)

How to: Highlight Document Syntax

The RichEditControl allows you to create a custom ISyntaxHighlightService (see ISyntaxHighlightService) implementation to display text in different colors and fonts according to the category of syntax sub-elements. These include keywords, comments, control-flow statements, variables, and other elements. This example describes how to highlight the T-SQL syntax.


The syntax highlight implementation can affect the application's performance.

Expanded Parse Document Into Tokens

A token represents a document range that should be highlighted. You can use third-party libraries or add custom syntax highlight logic to parse a document into tokens and highlight them. You can combine both approaches.


You can use the DevExpress CodeParser library to parse a document into tokens. Refer to the Syntax highlighting using DevExpress CodeParser and Syntax Highlight tokens for a code sample. Note that the library supports limited amount of languages.

Take into account the following requirements when you parse document into tokens:

  • Each range in a document should be marked by a token.
  • Tokens cannot intersect.
  • Tokens are continuous (start after the other token).
  • Tokens cannot mark fields, bookmarks or hyperlinks.

Follow the steps below to parse the document into tokens:

  1. Call the SubDocument.FindAll method to search for keywords or specific symbols.

    You can use regular expressions to search for a syntax. Specify the DocumentSearchOptions.RegExResultMaxGuaranteedLength property to extend the maximum length of a string that can be obtained in a regular expression search. Access the property via the richEditControl.Options.Search notation.

  2. Convert all occurrences to SyntaxHighlightToken objects. You can specify a token's format options in the object constructor.
  3. Check whether the tokens intersect. If not, add them to the tokens collection.
  4. Parse the remaining text into tokens and add them to the same collection.
  5. Sort the objects in the collection according to their position in the original text.

You can use the DocumentRange.Freeze or DocumentRangeExtensions (see DocumentRangeExtensions) class methods to improve performance during syntax highlight.

After one of these methods is called, RichEditControl stops tracking the actual document position for this range, and the target ranges cannot be modified. The frozen document ranges become invalid after the document is modified. Don't use these ranges for further document processing operations.

Expanded Specify Token's Format Options

The SyntaxHighlightProperties (see SyntaxHighlightProperties) class represents a token's format settings. You can pass this class's object to the DevExpress.XtraRichEdit.API.Native.SyntaxHighlightToken.%23ctor(System.Int32,System.Int32,DevExpress.XtraRichEdit.API.Native.SyntaxHighlightProperties) object constructor or use it as the SyntaxHighlightToken.Properties property value.

The code sample below converts keywords occurrences to the highlight tokens and specifies their foreground color:

Expanded Apply Syntax Highlight


The RichEditControl highlights unformatted text syntax only.

Call the SubDocument.ApplySyntaxHighlight within the ISyntaxHighlightService.Execute method to enable syntax highlighting.

The code sample below uses the ParseTokens method shown above to create a list of SyntaxHighlightToken objects and pass it to the ApplySyntaxHighlight method.

In the main class, use the DevExpress.XtraRichEdit.RichEditControl.ReplaceService``1 method to register the created implementation.

Expanded See Also

Is this topic helpful?​​​​​​​