Fastest way to append text to a richtextbox?

那年仲夏 提交于 2019-12-01 08:29:57
Jeremy Thompson

Use a StringBuilder and assign Text in one go.

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

' 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

Icsharpcode TextEditor


Here is how you do the scrolling to end with 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.

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

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).

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). This will cause the richtextbox to scroll to the end, so you do not need .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 .HideSelection to false and avoiding .ScrollToCaret greatly improved my program's speed.

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.

    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. Try that and see if that speeds up your process.

Unknown

Simply set .Visible to false, before adding lines of text.

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

Set .Visible back to true, when done adding lines.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!