Question

I have huge text files that I process line by line and add the results to a StringBuilder so I don't hold the main form with loading single lines of text to it.

Once processing is done I dump the result to a richtext textbox. I want to highlight some of the text based on keywords I have. I end up using a string. Find on all the text for each word to highlight it. I tried having a thread to highlight the text with lambda expression richbox.BeginInvoke. the thread works fine, but takes the handle over the rich text box and is really slow.

How do I loop through a richtext box line by line and highlight some words of it with understandable performance considering 50-100 MB of text?.

This question has been moved from super user because its programming relevant. There are some proposed solutions such as : http://www.dotnetcurry.com/ShowArticle.aspx?ID=146 and http://www.codeproject.com/Articles/4031/Background-Highlighting-with-the-RichTextBox-the-S , but they are still inefficient for large texts.

foreach (string x in LArgs)
{
    int len =0;
    int index = 0;
    int lastIndex=0;
    output.Invoke(() => { len=output.Text.Length; });
    output.Invoke(() => { lastIndex=output.Text.LastIndexOf(x); });
    while (index < lastIndex)
    {
        output.Invoke(() => { output.Find(x, index, len, RichTextBoxFinds.None); });
        output.Invoke(() => { this.output.SelectionBackColor = Color.Yellow; });
        output.Invoke(() => { index = this.output.Text.IndexOf(x, index) + 1; });
    }
}

I added the library and used this code:

scintilla1.Text = output.Text;

StringBuilder conf = new StringBuilder();
conf.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
conf.AppendLine(@"<ScintillaNET>");
conf.AppendLine("<Language Name=\"log\">");
conf.AppendLine("<lexer LexerName=\"log\">");
conf.AppendLine("<Keywords List=\"0\">");

foreach (string x in LArgs)
{
    conf.Append(x + " ");
}
//var
conf.AppendLine("</Keywords>");
conf.AppendLine(@"</lexer >");
conf.AppendLine(@"<Styles>");

conf.AppendLine(@"</Language>");
conf.AppendLine(@"</ScintillaNET>");

File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory+@"ScintillaNET.xml", conf.ToString());
scintilla1.Lexing.LexerLanguageMap["log"] = "cpp";
scintilla1.ConfigurationManager.CustomLocation = AppDomain.CurrentDomain.BaseDirectory + @"ScintillaNET.xml";
scintilla1.ConfigurationManager.Language = "log";
scintilla1.ConfigurationManager.Configure();

The text is loaded but it does not highlight any text or any text I add later on

Was it helpful?

Solution

Can you use extern libraries?

Then what about Scintilla.Net

It's a really good & fast highlighting control based on Scintilla (SciTE, Notepad++)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top