How to modify the syntax highlighting for predefined languages in the WinForms EditControl?
This article provides a step-by-step guide on configuring syntax highlighting for predefined languages in the WinForms EditControl.
By following these steps, highlight the syntax of the predefined languages with your desired color by using the formats and lexemes of the config language in EditControl.
Step 1: Create a configure language XML file for EditControl in the project. To set the configured language, use the ConfigLanguage tag attribute.
XML
<!--Initializes the Configuration language-->. <ConfigLanguage name="SQL"> <formats> </formats> <lexems> </lexems> </ConfigLanguage>
Step 2: Define the required formats with your desired color for each format in the config language for applying the syntax coloring to keywords, text, comments, and other elements as per the following code example.
XML
<!--Defines the formats-->. <ConfigLanguage name="SQL"> <formats> <format name="Text" Font="Courier New, 10pt" FontColor="Blue" /> <format name="Variable" Font="Courier New, 10pt" FontColor="Black" /> <format name="KeyWord" Font="Courier New, 10pt" FontColor="Green" /> <format name="Comment" Font="Courier New, 10pt, style=Bold" FontColor="Red" </formats> </ConfigLanguage>
Step 3: Define lexemes in the config language with the required words for each format using the BeginBlock tag with their types (format name) as per the following code sample.
XML
<!--Defines the lexemes by using Type-->. <ConfigLanguage name="SQL"> <formats> <format name="Text" Font="Courier New, 10pt" FontColor="Blue" /> <format name="Variable" Font="Courier New, 10pt" FontColor="Black" /> <format name="KeyWord" Font="Courier New, 10pt" FontColor="Green" /> <format name="Comment" Font="Courier New, 10pt, style=Bold" FontColor="Red" </formats> <lexems> <lexem BeginBlock="change" Type="KeyWord" /> <lexem BeginBlock="select" Type=" KeyWord " /> <lexem BeginBlock="+" Type="Operator" /> </lexems> </ConfigLanguage>
Step 4: Declare a string property to get the XML file path of configure language, then use the Configurator.Open the function to open the file for EditControl.
C#
private string configPath = Path.GetDirectoryName(Application.ExecutablePath) + @"\..\..\config.xml"; this.editControl1.Configurator.Open(configPath);
VB
private string configPath = Path.GetDirectoryName(Application.ExecutablePath) + @"\..\..\config.xml" Me.editControl1.Configurator.Open(configPath)
Step 5: Apply the appropriate language configuration for the EditControl by using the ApplyConfiguration method.
C#
//Apply the configuration in the SyntaxEditor. this.editControl1.ApplyConfiguration("SQL");
VB
//Apply the configuration in the SyntaxEditor. Me.editControl1.ApplyConfiguration("SQL")