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.
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 Edit Control?

 

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 the to each words 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

Sample:

http://websamples.syncfusion.com/samples/KB/Edit.Win/KB_KeywordsFromFile/main.htm

Article ID: Published Date: Last Revised Date: Platform: Control:
398 04/30/2011 07/10/2014 Windows Forms EditControl
Tags:
Did you find this information helpful?
Add Comment
You must log in to leave a comment

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