在填充巨大的TreeView时使用ProgressBar [英] Using ProgressBar while populating a huge TreeView

查看:52
本文介绍了在填充巨大的TreeView时使用ProgressBar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在树视图中填充了大量数据,通常程序需要一段时间才能将控制权交还给用户(我想这在树视图方面太繁琐了).因此,我决定提供一个进度条,以显示用户一切正常.

I am populating a treeview with a lot of data, and usually it takes a while for program to give back the control to user (I guess it is just too much process on the treeview side). So I decided to give a progress bar to show things are going fine to user.

在此树形视图中,我选择一个文件夹,该文件夹中的所有xml文件都将添加到该文件夹​​中,并且树形视图如下所示:

In this treeview I select a folder and all xml files in that folder will be added to this folder and the treeview looks like this:

+ FOLDER NAME 1
|__xml file name 1
|__xml file name 2
+FOLDER NAME 2
|__xml file name 1
|__xml file name 2

所以问题是,我的进度条实际上比树状视图本身快得多地完成,我认为是时候让程序可见以显示树状视图才是必需的时间.我该怎么办?

So problem is, my progressbar will be completed actually much faster than the treeview itself, I think it is the time for progam that is making the treeview visible that takes the must time. How can I do this?

这是我的代码,用于浏览文件夹并将其传递给函数以填充树状视图(对不起,它太长了,但我想让它变得更清楚),如您所见,我希望填充进度条的50%这一部分和其他50个节点,同时将节点添加到treeview:

This is my code for browing a folder and passing it to a function to populate treeview (Sorry taht is too long but I though it gonna make it more clear) As you see I want that 50% of progress bar to be filled in this part and other 50 while adding nodes to treeview:

        private void toolStripBrowseDirectory_Click(object sender, EventArgs e)
    {
        //Reset progressbar
        toolStripProgressBar.Value = 0;

        folderBrowser.ShowNewFolderButton = false;
        DialogResult result = folderBrowser.ShowDialog();

        int countAllfiles = 0;
        int countGoodFiles = 0;

        if (result == DialogResult.OK)
        {
            toolStripProgressBar.Value = 0;

            string selectedFolder = folderBrowser.SelectedPath;
            string[] files = Directory.GetFiles(selectedFolder, "*.xml", SearchOption.AllDirectories);
            List<string> goodFiles = new List<string>();

            toolStripProgressBar.Maximum = files.Length;

            countAllfiles = files.GetLength(0);

            //Folder name
            FileInfo folderInfo = new FileInfo(selectedFolder);
            string folderName = folderInfo.Name;

            if (countAllfiles != 0)
            {
                foreach (string file in files)
                {
                    //file name
                    FileInfo fileInfo = new FileInfo(file);
                    string fileName = fileInfo.Name;

                    try
                    {
                        XmlDocument checkFileType = new XmlDocument();
                        checkFileType.Load(file);

                        if (checkFileType.SelectSingleNode("//Document/@Type").Value == "Test Results")
                        {
                            countGoodFiles++;
                            goodFiles.Add(file);
                        }
                    }
                    catch (Exception a)
                    {
                        StartForm.setDebug("Comparison Mode", a.Message, 21, "Can not read Selected XML file '" + fileName + "'", "Check file for compatibilyty", "File was excluded from list");
                    }

                    this.toolStripProgressBar.PerformStep();
                }

                if (countGoodFiles == 0)
                {
                    MessageBox.Show("There are no compatible XML files (exported from ZTR files) in '" + folderName + "'", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    StartForm.setDebug("Comparison Mode", "N/A", 20, "There are no compatible XML files (exported from ZTR files) in '" + folderName + "'", "Select a folder that contains compatible files!", "No file has been added to Select File Tree");
                    toolStripProgressBar.Value = 100;
                }
                else if (countGoodFiles > 0)
                {
                    this.Cursor = Cursors.WaitCursor;

                    AddFilesToTree(folderName, goodFiles.ToArray());

                    this.Cursor = Cursors.Default;
                }
            }
            else
            {
                MessageBox.Show("There are no XML files in '" + folderName + "'", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);                    
            }
        }           
    }

这是用于填充树状视图

        private void AddFilesToTree(string folder, string[] files)
    {
        //Define folder node
        TreeNode folderNode = new TreeNode();
        folderNode.Text = folder;
        folderNode.Name = "folder";
        folderNode.Tag = folder;

        treeViewBrowsedFiles.Nodes.Add(folderNode);

        //Define file node
        foreach (string file in files)
        {
            try
            {
            //To check if the file is a valid test result (converted to XML from ZTR files)
            XmlDocument checkFileType = new XmlDocument();
            checkFileType.Load(file);

                LoadZTRCmp xml = new LoadZTRCmp(file);

                TreeNode fileNode = new TreeNode();
                fileNode.Text = xml.FileInfo("BatchNumber");
                fileNode.Name = "file";
                fileNode.Tag = file;

                folderNode.Nodes.Add(fileNode);

                this.toolStripProgressBar.PerformStep();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        folderNode.Toggle();
    }

那如何正确地做呢?

更新:** 解决了 **我发现了问题,在使用函数将节点添加到Treeview之前,我必须做这行!!!

Update: ** SOLVED ** I found the problem, I had to do this line before using function to add nodes to treeview!!!

                        toolStripProgressBar.Maximum += countGoodFiles;

                    AddFilesToTree(folderName, goodFiles.ToArray());

进度条的固定MAX值在TreeView开始加载之前,进度条仍会完成(已满).

fixed MAX value for progress bar Still progressbar finishes (maxed out) before TreeView starts to load...

推荐答案

       int progressSteps = (files.Length / 100)/2;

那是相当神秘的,但我当然不理解其逻辑.该代码很难阅读,您应该将其分解并使用更多的帮助器方法.尝试以下方法:

That's quite mysterious but I certainly don't understand the logic. The code is hard to read, you should break it up and use more helper methods. Try this instead:

        toolStripProgressBar.Maximum = files.Length;

这篇关于在填充巨大的TreeView时使用ProgressBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆