简体   繁体   English

将文本附加到richtextbox的最快方法?

[英]Fastest way to append text to a richtextbox?

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. 这个问题的原因是因为我想以正确的方式做事情,当我的richtextbox有很多行时,我不喜欢我的应用程序变慢。

Please, I need info, ideas and/or examples (no matter if in C# or VBNET). 请,我需要信息,想法和/或示例(无论是在C#还是VBNET中)。 Thankyou. 谢谢。

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 .HideSelection to false (it's true by default). 如果你需要格式化单个文本行,那么RichTextBox就是你要走的路,但是请.HideSelection你将.HideSelection转为false (默认情况下是真的)。 This will cause the richtextbox to scroll to the end, so you do not need .ScrollToCaret 这将导致.ScrollToCaret滚动到结尾,因此您不需要.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 UPDATE

To be more clear, setting .HideSelection to false and avoiding .ScrollToCaret greatly improved my program's speed. 更清楚的是,将.HideSelection设置为false并避免.ScrollToCaret大大提高了程序的速度。

Use a StringBuilder and assign Text in one go. 使用StringBuilder并一次性分配Text。

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 快速有色文本框换语法高亮

ScintillaNET ScintillaNET

Icsharpcode TextEditor Icsharpcode TextEditor


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 event Disclaimer: I dont work for any of these companies. 以下是如何使用Scintella.Net进行滚动Scintella.Net在文本更改过程中垂直滚动Scintilla文本框 免责声明:我 Scintella.Net 用于任何这些公司。

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

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;

The StringBuilder class was built for speed. StringBuilder类是为了速度而构建的。 Try that and see if that speeds up your process. 试试看,看看是否加快了你的过程。

Simply set .Visible to false , before adding lines of text. 在添加文本行之前,只需将.Visible设置为false

It will stop the Form from redrawing every time you add a line. 每次添加一行时,它都会停止重绘表单。

Set .Visible back to true , when done adding lines. 完成添加行后,将.Visible设置为true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM