【问题标题】:Highlighting rich text box with huge text efficiently in a windows form在 Windows 窗体中有效地突出显示带有大文本的富文本框
【发布时间】:2013-01-10 12:56:45
【问题描述】:

我有大量的文本文件,我逐行处理并将结果添加到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();

文本已加载,但没有突出显示任何文本或我稍后添加的任何文本

【问题讨论】:

  • 世界粮食计划署是一种选择吗?如果是这样,我使用 FlowDocument 执行此操作并在 BackGroundWorker 中创建它。我曾经在 Forms RichTextBox 上这样做,这很痛苦。
  • 解决方案应该向后兼容某些库:/

标签: c# .net winforms richtextbox scintilla


【解决方案1】:

你可以使用外部库吗?

那么Scintilla.Net

这是一个基于 Scintilla(SciTE、Notepad++)的非常好的快速高亮控件

【讨论】:

  • 我遇到了 XML 配置文件的问题,即单词“Lexer”。此外,这个框架最多支持 9 个自定义词,这在我的案例中是一个主要限制。
猜你喜欢
  • 2013-01-05
  • 2014-02-09
  • 2013-01-23
  • 1970-01-01
  • 2011-05-21
  • 2010-11-08
  • 2012-06-26
  • 2012-05-23
  • 1970-01-01
相关资源
最近更新 更多