C# 将文本附加到富文本框的最快方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16703901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:42:37  来源:igfitidea点击:

Fastest way to append text to a richtextbox?

c#.netvb.netrichtextbox

提问by ElektroStudios

I have a application with a RichTextBox control where a procedure is adding text almost all the time:

我有一个带有 RichTextBox 控件的应用程序,其中一个过程几乎一直在添加文本:

RichTextBox1.Text += vbNewLine & "Title: " & AlbumName
RichTextBox1.Text += vbNewLine & "Genre: " & AlbumGenre
RichTextBox1.Text += vbNewLine & "Year : " & AlbumYear
RichTextBox1.Text += vbNewLine & "Url  : " & AlbumLink

' The slow thing I think is here:
RichTextBox1.SelectionStart = RichTextBox1.Text.Length

RichTextBox1.ScrollToCaret

The problem is when the richtextbox has about more than 50 lines, when has more lines it turns more slowly to append the new text (obvious).

问题是当 Richtextbox 有大约 50 多行时,当有更多行时,它会更慢地附加新文本(很明显)。

I need to find a better way to accelerate the process, to loose at least a insignificant speed when richtextbox line-count reaches 1.000 (for example).

我需要找到一种更好的方法来加速这个过程,当 Richtextbox 行数达到 1.000(例如)时,至少要降低一个微不足道的速度。

The reason of this question is because I want to do the the things in the right way, I don't like my app to be slow when my richtextbox has much lines.

这个问题的原因是因为我想以正确的方式做这些事情,当我的富文本框有很多行时,我不喜欢我的应用程序变慢。

Please, I need info, ideas and/or examples (no matter if in C# or VBNET). Thankyou.

拜托,我需要信息、想法和/或示例(无论是在 C# 还是 VBNET 中)。谢谢你。

采纳答案by Jeremy Thompson

Use a StringBuilderand assign Text in one go.

使用 aStringBuilder并一次性分配文本。

Unless you rewrite the RichTextBox control I dont think you'll be able to speed up this function:

除非您重写 RichTextBox 控件,否则我认为您无法加速此功能:

' The slow thing I think is here:
RichTextBox1.SelectionStart = RichTextBox1.Text.Length 


For better speed consider these alternatives:

为了获得更好的速度,请考虑以下替代方案:

Fast-Colored-TextBox-for-syntax-highlighting

Fast-Colored-TextBox-for-syntax-highlighting

ScintillaNET

闪烁网

Icsharpcode TextEditor

icsharpcode 文本编辑器



Here is how you do the scrolling to end with Fast-Colored-TextBox-for-syntax-highlighting:

以下是滚动以结束的方式Fast-Colored-TextBox-for-syntax-highlighting

 Editor.ScrollLeft();
 Editor.Navigate(Editor.Lines.Count - 1);

Here is how you do the scrolling to end with Scintella.Net: Vertical scroll Scintilla Textbox during Text Changed eventDisclaimer: I dont work for any of these companies.

以下是滚动以结束的方式Scintella.Net在文本更改事件期间垂直滚动 Scintilla 文本框免责声明:我不为这些公司中的任何一家工作。

Update:

更新:

StringBuilder sb = new StringBuilder();
sb.AppendLine("Title: ");
sb.Append(AlbumName);
sb.AppendLine("Genre: ");
sb.Append(AlbumGenre);
sb.AppendLine("Year : ");
sb.Append(AlbumYear);
sb.AppendLine("Url  : ");
sb.Append(AlbumLink);
RichTextBox1.Text = sb.ToString();

回答by OneFineDay

The StringBuilderclass was built for speed. Try that and see if that speeds up your process.

StringBuilder的类是专为速度。尝试一下,看看这是否会加快您的进程。

回答by loopedcode

If first suggested option doesn't work for you, you can try the following. It's in C#, but I am sure you can convert it for VB.

如果第一个建议的选项不适合您,您可以尝试以下操作。它在 C# 中,但我相信您可以将其转换为 VB。

    StringBuilder text = new StringBuilder(RichTextBox1.Text);
    text.AppendFormat("{0}Title: {1}", Environment.NewLine, AlbumName);
    text.AppendFormat("{0}Genre: {1}", Environment.NewLine, AlbumGenre);
    text.AppendFormat("{0}Year: {1}", Environment.NewLine, AlbumYear);
    text.AppendFormat("{0}Url: {1}", Environment.NewLine, AlbumLink);

    RichTextBox1.Text = text.ToString();
    RichTextBox1.SelectionStart = RichTextBox1.Text.Length;
    RichTextBox1.ScrollToCaret;

回答by Cordell

This is an older post - but I wanted to help out future generations!

这是一篇较旧的帖子 - 但我想帮助后代!

I've been having the SAME issue - and finally found a solution... First off, if you do not need the extra formatting use a TextBox instead (from my studies, it's faster and auto-scrolls to the end).

我一直有同样的问题 - 终于找到了解决方案......首先,如果您不需要额外的格式,请改用 TextBox (根据我的研究,它更快并且自动滚动到最后)。

If you need the formatting of individual lines of text, RichTextBox is the way to go, but MAKE SURE you turn .HideSelectionto false(it's true by default). This will cause the richtextbox to scroll to the end, so you do not need .ScrollToCaret

如果您需要对单个文本行进行格式化,RichTextBox 是可行的方法,但请.HideSelection确保您转向false(默认情况下为 true)。这将导致richtextbox滚动到最后,所以你不需要.ScrollToCaret

Here is what I am using after I've set all the property values for the rich textbox:

这是我为富文本框设置所有属性值后使用的内容:

private void appendOutput(String msg){
    richTextBoxOutput.AppendText(msg + "\r\n");
}


private void appendError(String msg, bool clearPrior){
    if (clearPrior){
        richTextBoxOutput.Clear();
    }

    richTextBoxOutput.SelectionColor = Color.Red;
    richTextBoxOutput.SelectedText = msg + "\r\n";
}

UPDATE

更新

To be more clear, setting .HideSelectionto falseand avoiding .ScrollToCaretgreatly improved my program's speed.

更清楚地说,设置.HideSelectionfalse和避免.ScrollToCaret大大提高了我的程序的速度。

回答by Unknown

Simply set .Visibleto false, before adding lines of text.

在添加文本行之前,只需设置.Visiblefalse, 即可。

It will stop the Form from redrawing every time you add a line.

每次添加一行时,它都会阻止表单重新绘制。

Set .Visibleback to true, when done adding lines.

添加行.Visibletrue,设置回。