Updated 11.02.16

How can I display large (several hundred megabytes) text in a form? TextBox and RichTextBox do not cope with a similar volume.

Here are some simple code examples for demonstration:

 StringBuilder sb = new StringBuilder(); for (int i = 0; i < 10000000; i++) { sb.Append(i); sb.Append('\n'); } this.textBox1.Text = sb.ToString(); 

The cycle of forming the line is performed fairly quickly (seconds), but on transferring text to the TextBox, everything rises tightly, there was enough patience for 5 minutes, then it stopped forcibly.

 string[] lines = string[10000000]; for (int i = 0; i < 10000000; i++) { lines[i] = i.ToString(); } this.textBox1.Lines = lines; 

The result is similar

 StringBuilder sb = new StringBuilder(); for (int i = 0; i < 10000000; i++) { sb.Append(i); sb.Append('\n'); } this.richTextBox1.Text = sb.ToString(); 

The result is slightly better, the control is filled and even drawn, but when scrolling the text it also freezes after the first hundred lines.

There are two problems:

  • organizing a buffer suitable for reading / writing to a file and editing in a TextBox ;
  • organization of the correct operation of the vertical scrolling TextBox , taking into account the size of the file, and not just the displayed fragment.

"Excavations" in the documentation did not give any useful result. So far, only one idea has arisen: read the file into an array of strings and give it to the TextBox in portions, but at the same time I don’t understand how to force the native TextBox display the actual file size and perform the movement accordingly.

Another option is to build your “bike” with “Preference and Young Ladies”, but I would not like it if there are solutions for standard controls. In any case, I will not give up ideas on how to organize text editing and related tasks, such as tracking and fixing the cursor position in the control.

EnSO was also dug up in search of an answer, one of the recommendations is to use special software for this. But if there is such software, then the problem is still solved.

  • Why TextBox ? I'm not sure about WinForms, but WPF TextBox not intended for huge amounts of text. - VladD
  • @VladD so there is nothing more standard, well, except for RichBox with the ability to edit. I do not exclude that I don’t stand by the problem, so I ask here. Perhaps there are third-party libraries, but this is an extreme measure, if nothing comes up at all. - rdorn
  • Probably, you can manually paginate and show page by page. This, of course, a harsh bicycle, will have to be reflow after every text change. I have not heard of the decision out of the box. - VladD
  • one
    @VladD wait, can anyone come across and ready to share. Well, or otpishus himself when I win - rdorn
  • I would love to know how to solve this problem. Surely useful. - VladD

2 answers 2

As an option, read the file line by line and fill in as needed. To onScroll function to the onScroll event of the onScroll element, which will load new lines from the file. Trying to download a large file entirely in some kind of control is problematic.

For example, when working with a database, they usually either use Limit and load N lines, or load the entire request (limited only by RAM) into a class buffer, and then pull up the lines from it into the Grid on the form.

  • Virtualization, in general. And what to do with the scrolling position? - VladD
  • It is not necessary to track the position, just load in small portions (for example, 10 lines, so that the friezes would not be visible) at each scrolling event to the bottom until the lines run out. - SYL
  • More difficult with editing (if after this you need to save the file). The only option is to keep the entire source file in a buffer, and after editing, replace all loaded lines with those that you change in the control on the form (you will still save the index of the line obtained from the buffer to load the lines). - SYL
  • Well, another option is to find a custom control that supports virtualization, make a class buffer with all the necessary methods and use DataBinding - SYL
  • Hmm, I meant that you have to somehow manually set the position to the internal scrollbar. By default, it assumes that the data is fully loaded and shows the position accordingly. - VladD

On SOeN , at one time, they offered a variant through RichTextBox , for files with a large size.

 Use RichTextBox.LoadFile http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.loadfile.aspx I'm not sure why you would want to load the entire text to a StringBuilder. Alternatively you could pass a FileStream to LoadFile which would render the large file for you. 

(C) AB Kolan

Just as an option, you can read the reasoning on Social.MSDN

  • Yes, I saw this answer. take a closer look, in the last code example, just rtb. he, too, did not master such a volume, alas - rdorn
  • @rdorn Although you have rtb in the last code example, but still, the answer indicates that you should try not StringBuilder() but .LoadFile , but specifically about StringBuilder() , I suggested a link with arguments, there As a result, in rtb files of more than 400mb in size they came up with loading byte dimensions per page with further loading when scrolling - LamerXaKer
  • I have not yet seen the last link, but a similar approach has been proposed in the question. to him still to solve problems with scrolling and buffer. buffer okay, the problem is not great, with scrolling worse - rdorn
  • @rdorn In the question, yes, the approach is similar, and it did not work in the same way, therefore they suggested in the answer try using .LoadFile - LamerXaKer