开发者

loading large text file into a richtextbox; reacting to textbox scroll

开发者 https://www.devze.com 2023-03-29 07:54 出处:网络
I was thinking somehow to load the text file (6MB) in parts so each time the user will scroll down it will load the next part of the text file into the richtextbox.

I was thinking somehow to load the text file (6MB) in parts so each time the user will scroll down it will load the next part of the text file into the richtextbox.

I don't know how to check if and when the user scrolled down and how much does he scrolled down and then how much to load again from the text file to the richtextbox.

Also i want that the progressbar will show percentages each time the user scroll down and its loading the next part. So they user will know how long it's going to take to upload the next part of the text.

Maybe there is another way to solve it. I want the best way to load the 6mb text file to the richtextbox best way i mean fastest way. or if not so fast so the progressbar which is not working good now will show how much left.

I used a counter there before just to check if he was >= 200 then break; I don't need it now, I want to solve some how the large file loading problem.

namespace WindowsFormsApplication1
{
    public partial class textBoxLoggerViewer : Form
    {
        int counter;
        StreamReader sr;
        string line;
        string log_file开发者_如何学Go_name = @"\logger.txt";
        string logger_file_to_read = Path.GetDirectoryName(Application.LocalUserAppDataPath) + @"\log";

        public textBoxLoggerViewer()
        {
            InitializeComponent();
            counter = 0;
            richTextBox1.Font = new Font("Consolas", 8f, FontStyle.Bold);
            richTextBox1.BackColor = Color.AliceBlue;
            backgroundWorker1.WorkerReportsProgress = true;
            backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
            backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
            backgroundWorker1.RunWorkerAsync();
        }

        private void textBoxLoggerViewer_Load(object sender, EventArgs e)
        {
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            sr = new StreamReader(logger_file_to_read+log_file_name);
            while (null != (line = sr.ReadLine()))
              {
                      counter = counter + 1;
                      backgroundWorker1.ReportProgress(1, line);
              }
            sr.Close();
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
            richTextBox1.AppendText(e.UserState.ToString());
        }

    }
}


You can get a hint of how to determine the number of lines the user scrolled or which lines he is looking from the answer to this question:

Winforms RichTextBox: How can I determine how many lines of text are visible?

In order to properly show progress, you should read the contents of the file chunk by chunk, e.g. if you are reading 6M, then read in chunks of 300k, this way your progress will go 5%, 10%, 15% ...

0

精彩评论

暂无评论...
验证码 换一张
取 消