6

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.

ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
  • Just a little tip with VB.Net code comments. I personally have an apostrophe at the end of the comment. It doesn't make much difference in code (other than the it looks slightly cleaner) but it prevents text boxes in web pages from thinking it is just a string (which is what your answer has done). It is slightly nitpicky but it looks so much better and your code will be properly coloured. – James Yeoman Jan 31 '17 at 10:32
  • If you only want to add text to the end, consider using RichTextBox.AppendText. – Harald Coppoolse Mar 02 '17 at 09:42

5 Answers5

16

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.

Cordell
  • 1,901
  • 1
  • 13
  • 12
7

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();
Community
  • 1
  • 1
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • Can you please add in your answer a StringBuilder example taking by reference my example code lines? How it would be using stringbuilder? I directly replace the richtextbox text with the stringbuilder text or...how? – ElektroStudios May 23 '13 at 01:51
  • Thanks, I'm out of time now I will try all answers later also "fast colored" and accept an answer, thanks again. – ElektroStudios May 23 '13 at 02:06
  • StringBuilder was really fast, when adding large chunks of "string-data" to the RichTextBox – Half_Baked Dec 02 '13 at 00:02
  • 1
    Why aren't you using `sb.AppendLine(...)` instead of `sb.Append(Environment.NewLine + ...)`? – Chad Dec 21 '16 at 15:53
  • Shouldn't this `RichTextBox1.Text = sb.ToString();` be like `RichTextBox1.Text += sb.ToString();` ? You are resetting richtextbox' text everytime, no? – Everyone Dec 22 '16 at 02:10
  • 1
    No the trick is to do all concatenation using a stringbuilder. Assigning it once it key because the RichTextBox's Text property is string and strings are immutable. – Jeremy Thompson Dec 22 '16 at 02:12
4

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;
interesting-name-here
  • 1,851
  • 1
  • 20
  • 33
loopedcode
  • 4,863
  • 1
  • 21
  • 21
3

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

OneFineDay
  • 9,004
  • 3
  • 26
  • 37
  • 2
    Can you please add in your answer a StringBuilder example taking by reference my example code lines? How it would be using stringbuilder? I directly replace the richtextbox text with the stringbuilder text or...how? – ElektroStudios May 23 '13 at 01:51
1

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.

Abhinav Sood
  • 799
  • 6
  • 23
Unknown
  • 11
  • 1