[Expand]General Information
[Expand]WinForms Controls
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Collapse]WPF Controls
 [Expand]What's Installed
 [Expand]Common Concepts
 [Expand]MVVM Framework
 [Collapse]Controls and Libraries
  [Expand]Data Grid
  [Expand]Ribbon, Bars and Menu
  [Expand]Charts Suite
  [Expand]Pivot Grid
  [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
    Mail Merge
    Restrictions and Protection
    Syntax Highlighting
   [Expand]Visual Elements
    HTML Tag Support
  [Expand]Tree List
  [Expand]Gauge Controls
  [Expand]Map Control
  [Expand]Layout Management
  [Expand]Windows Modern UI
  [Expand]Data Editors
  [Expand]Navigation Controls
  [Expand]Spell Checker
  [Expand]Property Grid
  [Expand]PDF Viewer
  [Expand]TreeMap Control
  [Expand]Gantt Control
  [Expand]Diagram Control
  [Expand]Windows and Utility Controls
   Dialogs, Notifications and Panels
  [Expand]Scheduler (legacy)
 [Expand]Scaffolding Wizard
  Redistribution and Deployment
  Get More Help
 [Expand]API Reference
[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)

Syntax Highlighting

Syntax highlighting enables you to display text in different colors and fonts according to the category of text tokens.

Expanded Overview

To accomplish this task in the RichEditControl, you should perform the following steps:

  • Make sure that the RichEditControl's document contains only plain text.
  • Parse text into tokens according to certain terms or specific grammar.
  • Sort the collection of tokens so that the tokens are ordered by their start position in the document.
  • Pass the ordered collection to the SubDocument.ApplySyntaxHighlight method.

A correct implementation involves the use of a custom service that implements the ISyntaxHighlightService interface. Its ISyntaxHighlightService.Execute method should create a collection of tokens for the current document and call the ApplySyntaxHighlight method. The custom syntax highlighting service is registered as usual, by using the DevExpress.XtraRichEdit.RichEditControl.ReplaceService``1 method.

Expanded What is Token?

A token used in syntax highlighting is a range of text (defined by its start position and length) with format settings. Format settings specify how this range of text should be displayed. A token is an object of the SyntaxHighlightToken type, its format settings are contained within the object of the SyntaxHighlightProperties type.

The token itself does not contain any portion of document's text. Only start position and length matter. It means that the original text should not contain complex structures such as fields, which can shift the document position. Bookmarks or hyperlinks are also not allowed.


Do not load formatted text in the RichEditControl with syntax highlighting.

The following piece of code illustrates how the plain text can be parsed into tokens. We search for text enclosed with quotation marks, then for keywords. The remaining parts of the text are marked as plain tokens to fill gaps in token continuity. The resulting token collection should be ordered by its position in the text.

Expanded DevExpress.CodeParser Library

A custom search for keywords is not the fastest way to parse text into tokens. You can use the DevExpress.CodeParser library to accomplish this task more effectively.

The code parser library is contained within the DevExpress.CodeParser.v19.1.dll assembly. Currently, it can be utilized to parse text in the following languages:

  • C#
  • Visual Basic
  • JavaScript
  • HTML
  • XAML
  • CSS

The library contains token category helper objects designed to parse text into a collection of tokens. The helper object is different for different languages, but exposes the same DevExpress.CodeParser.ITokenCategoryHelper interface. To obtain the helper object, use the static methods of the DevExpress.CodeParser.TokenCategoryHelperFactory class. The CreateHelperForFileExtensions(string extension) method creates a helper for the language determined by the file extension. The CreateHelper(ParserLanguageID languageID) method requires a language ID to create a proper helper. You can use another helper class, the DevExpress.CodeParser.ParserLanguage and its FromFileExtension method to obtain a language ID for the specified extension.

This technique is illustrated by the following code snippet.

Expanded Apply Syntax Highlight

When the text is parsed into tokens, you should specify the desired color and other formatting for each token according to its type, via the SyntaxHighlightToken.Properties. After that, the collection of tokens has to be ordered by the token position in the original text (a mandatory requirement). To display the text as the token formatting suggests, you should call a special method named SubDocument.ApplySyntaxHighlight.

This method changes formatting for the text visible in the RichEditControl's view. A special ISyntaxHighlightService service operates to ensure that formatting is always applied to the text in situations when the text is modified or the visible region is changed. You should call the ApplySyntaxHighlight method within the ISyntaxHighlightService.Execute method of this service, as illustrated in the DevExpress.CodeParser Library section earlier in this document.

To accomplish this task, you should create a custom service inherited from the ISyntaxHighlightService and implement the Execute and the ForceExecute methods. To register a custom service, use the DevExpress.XtraRichEdit.RichEditControl.ReplaceService``1 method.

The picture below illustrates syntax highlighting for the text in C# displayed within the RichEditControl's SimpleView with custom line numbering.

Expanded See Also

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