在窗体中高效突出显示带有大文本的富文本框

时间:2013-01-26 19:42:01

标签: c# .net winforms richtextbox scintilla

我有大量的文本文件,我逐行处理并将结果添加到StringBuilder,所以我不保持主窗体加载单行文本。

处理完成后,我将结果转储到richtext textbox。我想根据我的关键词突出显示一些文字。我最终使用了一个字符串。查找每个单词的所有文本以突出显示它。我尝试使用一个线程来突出显示lambda表达式richbox.BeginInvoke的文本。线程工作正常,但在富文本框上占用句柄并且非常慢。

如何考虑50-100 MB的文本,逐行循环遍历richtext box并突出显示其中的一些单词并具有可理解的性能?

此问题已从超级用户转移,因为其编程相关。 有一些建议的解决方案,例如: http://www.dotnetcurry.com/ShowArticle.aspx?ID=146http://www.codeproject.com/Articles/4031/Background-Highlighting-with-the-RichTextBox-the-S ,但对于大文本来说,它们仍然效率低下。

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; });
    }
}

我添加了库并使用了这段代码:

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();

文本已加载但未突出显示我稍后在

添加的任何文字或文字

1 个答案:

答案 0 :(得分:1)

你能使用extern库吗?

那么Scintilla.Net

呢?

这是一个非常好的&amp;基于Scintilla(SciTE,Notepad ++)的快速突出显示控件