)
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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to load words from the file and and set as keywords dynamically in WinForms SyntaxEditor (EditControl)?

Platform: WinForms |
Control: EditControl |
Published Date: April 30, 2011 |
Last Revised Date: December 12, 2019

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

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
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
Live Chat Icon