What would be the best way to show the contents of a large text file to the user using Winforms? Right now I've tried a multiline Textbox, but this is rather slow for a 2MB file.
Depends on what type of data.
You will hardly find something better for a pure textfile as the TextBox or RichtTextBox.
Hmm. Interesting. Time for an experiment.
I whipped up a .Net winforms project with a button that loads a 50k line text file from my harddrive into a List. This part is almost instantaneous with a StreamReader.
Loading the lines in my standard run-of-the-microsoft-mill multiline textbox happened pretty quickly:
var lines = new List<string>();
using (var sr = new StreamReader(@"C:\temp\lotsoftext.txt"))
{
while (!sr.EndOfStream) lines.Add(sr.ReadLine());
}
TextBox.Lines = lines.ToArray();
I didn't time it, but it took at most a second.
When I tried to do the same in the rich textbox the system froze on me. It did put the text in the RichTextbox, but I grew a beard while waiting for it. My uneducates guess would be that all this time is spent parsing the input for markup, so using a plain textbox for plain text would be best.
Sounds like you might want to consider writing your own custom control for doing this -- you can then "optimise out" some of the reasons for it being slow (for instance, you might know that lines should never wrap, which will simplify the algorithm for working out the scroll bar proportions)