Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the fastest way to load text file into RichTextBox?

I load text file into RichTextBox using OpenFIleDialog. But when a large amount of text is (for example song text about 50-70 lines) and I click OPEN program hangs for a few second (~3-5). Is it normal? Maybe there is some faster way or component to load text file? If my question is an inappropriate just delete it. Thanx.

if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            string text = File.ReadAllText(openFileDialog1.FileName);
            for (int i = 0; i < text.Length - 1; i++)
            {
                richTextBox1.Text = text;
            }
        }

I guess maybe ReadAllLines impeds it?

like image 643
Frankie Drake Avatar asked Mar 16 '11 09:03

Frankie Drake


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 the file format exclusively used in RichTextBox control?

The Windows Forms RichTextBox control can display a plain-text, Unicode plain-text, or Rich-Text-Format (RTF) file. To do so, call the LoadFile method. You can also use the LoadFile method to load data from a stream.

What is RichTextBox?

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


1 Answers

There is a similar question that deals with the fastest way of reading/writing files: What's the fastest way to read/write to disk in .NET?

However, 50-70 lines is nothing.. no matter how you read, it should fly in immediately. Are you maybe reading from a network share or something else that is causing the delay?

Edit: Now that I see your code: Remove the loop and just write richTextBox1.Text = text; once. It doesn't make sense to assign the string in the loop since you already read the complete content of the file by using ReadAllText.

if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
    string text = File.ReadAllText(openFileDialog1.FileName);
    richTextBox1.Text = text;
}
like image 96
Christian Avatar answered Oct 05 '22 22:10

Christian