Category / Section
How to load words from the file and and set as keywords dynamically in WinForms SyntaxEditor (EditControl)?
2 mins read
Custom configuration language
To set the words from the file as keywords dynamically, we need to use custom configuration language and need to create custom keyword format and apply to each word in the file.
C#
string path = @"..\\..\\temp\\test.txt"; this.editControl1.Font = new Font("Times New Roman", 13); //Create a new configuration language - Script IConfigLanguage currentConfigLanguage = this.editControl1.Configurator.CreateLanguageConfiguration("Script"); this.editControl1.ApplyConfiguration(currentConfigLanguage); //Create a new format - Keyword and set its attributes ISnippetFormat keywordFormat = this.editControl1.Language.Add("Keyword"); keywordFormat.FontColor = Color.Blue; keywordFormat.Font = new Font(this.editControl1.Font.FontFamily, this.editControl1.Font.Size); string str = ""; using (FileStream fs = File.Open(path, FileMode.Open)) { byte[] b = new byte[1024]; UTF8Encoding temp = new UTF8Encoding(true); while (fs.Read(b, 0, b.Length) > 0) { str = temp.GetString(b); } } char[] ch = { ''\r'', ''\n'' }; string[] str1= str.Split(ch,StringSplitOptions.RemoveEmptyEntries); //Create a lexem - this that belongs to the format - Keyword foreach(string s in str1) { configLex[i] = new ConfigLexem(s, "", FormatType.Custom, false); configLex[i].IsBeginRegex = true; configLex[i].FormatName = "Keyword"; this.editControl1.Language.Lexems.Add(configLex[i]); //Add it to the current language''s lexems collection this.editControl1.Language.ResetCaches(); //Apply the modified configuration language this.editControl1.ApplyConfiguration(this.editControl1.Language); i++; }
VB
Dim path As String = "..\\..\\temp\\test.txt" Me.editControl1.Font = New Font("Times New Roman", 13) ''Create a new configuration language - Script Dim currentConfigLanguage As IConfigLanguage = Me.editControl1.Configurator.CreateLanguageConfiguration("Script") Me.editControl1.ApplyConfiguration(currentConfigLanguage) ''Create a new format - Keyword and set its attributes Dim keywordFormat As ISnippetFormat = Me.editControl1.Language.Add("Keyword") keywordFormat.FontColor = Color.Blue keywordFormat.Font = New Font(Me.editControl1.Font.FontFamily, Me.editControl1.Font.Size) Dim str As String = "" Using fs As FileStream = File.Open(path, FileMode.Open) Dim b() As Byte = New Byte(1023){} Dim temp As UTF8Encoding = New UTF8Encoding(True) Do While fs.Read(b, 0, b.Length) > 0 str = temp.GetString(b) Loop End Using Dim ch() As Char = { ControlChars.Cr, ControlChars.Lf } Dim str1() As String= str.Split(ch,StringSplitOptions.RemoveEmptyEntries) '' Create a lexem - this that belongs to the format - Keyword For Each s As String In str1 configLex(i) = New ConfigLexem(s, "", FormatType.Custom, False) configLex(i).IsBeginRegex = True configLex(i).FormatName = "Keyword" Me.editControl1.Language.Lexems.Add(configLex(i)) ''Add it to the current language''s lexems collection Me.editControl1.Language.ResetCaches() ''Apply the modified configuration language Me.editControl1.ApplyConfiguration(Me.editControl1.Language) i += 1 Next s
Reference link: https://help.syncfusion.com/windowsforms/syntax-editor/syntax-highlighting#custom-language-using-xml