Category / Section
How to programmatically perform syntax highlighting in the WinForms SyntaxEditor (EditControl)?
2 mins read
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.
C#
public Form1() { InitializeComponent(); IConfigLanguage language = this.editControl1.Configurator.CreateLanguageConfiguration("New"); Split s1 = new Split("/*"); Split s2 = new Split("*/"); language.Splits.Add(s1); language.Splits.Add(s2); this.editControl1.ApplyConfiguration(language); 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"; this.editControl1.Language.Lexems.Add(keywordlexem1); ConfigLexem keywordlexem2 = new ConfigLexem("int", "", FormatType.Custom, false); keywordlexem2.FormatName = "Keyword"; this.editControl1.Language.Lexems.Add(keywordlexem2); ConfigLexem configLex = new ConfigLexem("this", "", FormatType.Custom, false); configLex.FormatName = "Keyword"; this.editControl1.Language.Lexems.Add(configLex); 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.Language.Lexems.Add(commentlexem); this.editControl1.Language.ResetCaches(); this.editControl1.Text = "this is the first line\nthis is the second line\nthis is the third line\n\n/* Sample */\n\nprivate int"; }
VB
Public Sub New() ' This call is required by the Windows Form Designer. InitializeComponent() Dim language As IConfigLanguage = Me.EditControl1.Configurator.CreateLanguageConfiguration("New") Dim s1 As Split = New Split("/*") Dim s2 As Split = New Split("*/") language.Splits.Add(s1) language.Splits.Add(s2) Me.EditControl1.ApplyConfiguration(language) 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" Me.EditControl1.Language.Lexems.Add(keywordlexem1) Dim keywordlexem2 As ConfigLexem = New ConfigLexem("int", "", FormatType.Custom, False) keywordlexem2.FormatName = "Keyword" Me.EditControl1.Language.Lexems.Add(keywordlexem2) Dim configLex As ConfigLexem = New ConfigLexem("this", "", FormatType.Custom, False) configLex.FormatName = "Keyword" Me.EditControl1.Language.Lexems.Add(configLex) 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.Language.Lexems.Add(commentlexem) Me.EditControl1.Language.ResetCaches() 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