Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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

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

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.

like image 271
ElektroStudios Avatar asked May 23 '13 00:05

ElektroStudios


People also ask

How do I add text to RichTextBox?

Step 2: Drag the RichTextBox control from the ToolBox and drop it on the windows form. You are allowed to place a RichTextBox control anywhere on the windows form according to your need. Step 3: After drag and drop you will go to the properties of the RichTextBox control to add text in the RichTextBox control.

What is difference between RichTextBox and TextBox?

The RichTextBox is similar to the TextBox, but it has additional formatting capabilities. Whereas the TextBox control allows the Font property to be set for the entire control, the RichTextBox allows you to set the Font, as well as other formatting properties, for selections within the text displayed in the control.

Can RichTextBox display HTML?

Presents code to display bindable HTML text in a WPF RichTextBox or a WebBrowser.

What does a rich text box do?

The RichTextBox control enables you to display or edit flow content including paragraphs, images, tables, and more. This topic introduces the TextBox class and provides examples of how to use it in both Extensible Application Markup Language (XAML) and C#.


2 Answers

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.

like image 72
Cordell Avatar answered Sep 29 '22 03:09

Cordell


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();
like image 22
Jeremy Thompson Avatar answered Sep 29 '22 03:09

Jeremy Thompson