Category / Section
How to programmatically configure syntax highlighting in WinForms SyntaxEditor?
3 mins read
WinForms SyntaxEditor allows to configure syntax formatting of built-in supported languages and also it allows to create and configure new languages using xml and programmatically. This article explains how to programmatically configure syntax highlighting after custom configuration loaded using EditControl.Configurator.Open.
In SyntaxEditor, you can change the font style, font family, font size and font color of syntax highlighting by creating ConfigLexem and adding it to EditControl.Language.Lexems collection.
Creating custom configuration lexems
Follow below steps for creating custom configuration
- Create new format using EditControl.Language.Add method.
- Configure font settings for newly created format.
- Create ConfigLexem and set the about create format and configure its attribute. For example, in the below code two lexem are created. One lexem to format the string A to Z and another formats the numbers 1 to 9 using RegEx.
- Add the configured lexem to active Language configuration.
C#
//creating new format ISnippetFormat keywordFormat = this.editControl1.Language.Add("Keyword"); //Configuring newly created format keywordFormat.FontColor = Color.Red; keywordFormat.Font = new Font(FontFamily.GenericSansSerif, 12, FontStyle.Regular); //creating new ConfigLexem ConfigLexem configLex = new ConfigLexem("[A-Z]+", "", FormatType.Custom, false); configLex.IsBeginRegex = true; configLex.IsEndRegex = true; configLex.FormatName = "Keyword"; //creating new format ISnippetFormat keywordFormat1 = this.editControl1.Language.Add("Keyword1"); // Configuring newly created format keywordFormat1.FontColor = Color.Green; keywordFormat1.Font = new Font(FontFamily.GenericSansSerif, 12, FontStyle.Bold); //creating new ConfigLexem ConfigLexem configLex1 = new ConfigLexem("[1-9]+", "", FormatType.Custom, false); configLex1.IsBeginRegex = true; configLex1.IsEndRegex = true; configLex1.FormatName = "Keyword1"; //adding ConfigLexems to language editControl1.Language.Lexems.Add(configLex); editControl1.Language.Lexems.Add(configLex1); editControl1.Language.ResetCaches();
Sample : View Sample in GitHub