We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to programmatically perform syntax highlighting in the WinForms SyntaxEditor (EditControl)?

Platform: WinForms |
Control: EditControl

Syntax highlighting

It is possible to create configuration settings programmatically using ISnippetFormat and ConfigLexem class. Please refer to the attached sample and the code snippets below that illustrates this.


public Form1()
    IConfigLanguage language = this.editControl1.Configurator.CreateLanguageConfiguration("New");
    Split s1 = new Split("/*");
    Split s2 = new Split("*/");
    ISnippetFormat keyword = this.editControl1.Language.Add("Keyword");
    keyword.Font = new Font("Arial", 10, FontStyle.Regular);
    keyword.FontColor = Color.Blue;
    ISnippetFormat comment = this.editControl1.Language.Add("Comment");
    comment.Font = new Font("Arial", 10, FontStyle.Regular);
    comment.FontColor = Color.Green;
    ISnippetFormat text = this.editControl1.Language.Add("Text");
    text.FontColor = Color.Brown;
    text.Font = new Font("Arial", 10);
    ConfigLexem keywordlexem1 = new ConfigLexem("private", "", FormatType.Custom, false);
keywordlexem1.FormatName = "Keyword";
    ConfigLexem keywordlexem2 = new ConfigLexem("int", "", FormatType.Custom, false);
    keywordlexem2.FormatName = "Keyword";
    ConfigLexem configLex = new ConfigLexem("this", "", FormatType.Custom, false);
    configLex.FormatName = "Keyword";
    ConfigLexem commentlexem = new ConfigLexem("/*","*/", FormatType.Custom,true);
    commentlexem.FormatName = "Comment";
    commentlexem.OnlyLocalSublexems = true;
    ConfigLexem configLex1 = new ConfigLexem("[a-z|A-Z]*", "", FormatType.Custom, false);
    configLex1.IsBeginRegex = true;
    configLex1.FormatName = "Text";
    this.editControl1.Text = "this is the first line\nthis is the second line\nthis is the third line\n\n/* Sample */\n\nprivate int";



Public Sub New()
   ' This call is required by the Windows Form Designer.
   Dim language As IConfigLanguage =   Me.EditControl1.Configurator.CreateLanguageConfiguration("New")
   Dim s1 As Split = New Split("/*")
   Dim s2 As Split = New Split("*/")
   Dim keyword As ISnippetFormat = Me.EditControl1.Language.Add("Keyword")
   keyword.Font = New Font("Arial", 10, FontStyle.Regular)
   keyword.FontColor = Color.Blue
   Dim comment As ISnippetFormat = Me.EditControl1.Language.Add("Comment")
   comment.Font = New Font("Arial", 10, FontStyle.Regular)
   comment.FontColor = Color.Green
   Dim text As ISnippetFormat = Me.EditControl1.Language.Add("Text")
   text.FontColor = Color.Brown
   text.Font = New Font("Arial", 10)
   Dim keywordlexem1 As ConfigLexem = New ConfigLexem("private", "", FormatType.Custom, False)
   keywordlexem1.FormatName = "Keyword"
   Dim keywordlexem2 As ConfigLexem = New ConfigLexem("int", "", FormatType.Custom, False)
   keywordlexem2.FormatName = "Keyword"
   Dim configLex As ConfigLexem = New ConfigLexem("this", "", FormatType.Custom, False)
   configLex.FormatName = "Keyword"
   Dim commentlexem As ConfigLexem = New ConfigLexem("/*", "*/", FormatType.Custom, True)
   commentlexem.FormatName = "Comment"
   commentlexem.OnlyLocalSublexems = True
   Dim configLex1 As ConfigLexem = New ConfigLexem("[a-z|A-Z]*", "", FormatType.Custom, False)
   configLex1.IsBeginRegex = True
   configLex1.FormatName = "Text"
   Me.EditControl1.Text = "this is the first line" & Constants.vbLf & "this is the second line" &   Constants.vbLf & "this is the third line" & Constants.vbLf + Constants.vbLf & "/* Sample */" & Constants.vbLf + Constants.vbLf & "private int"
   End Sub
End Class


Reference link: https://help.syncfusion.com/windowsforms/syntaxeditor/syntax-highlighting

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile