Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4249710
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:25:58+00:00 2026-05-21T04:25:58+00:00

I am populating a treeview with a lot of data, and usually it takes

  • 0

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.

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?

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);                    
            }
        }           
    }

And this is for poulating the treeview

        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();
    }

So how to do this right way?

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());

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T04:25:58+00:00Added an answer on May 21, 2026 at 4:25 am
           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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have TreeView Populating from Database And I am Trying to detect the user
I am populating a treeview control, c# visual studio 8, using this code: private
I've created a (System.Windows.Controls.Primitives.)Popup that contains a Treeview that I'm populating with data read
Initially I am populating 100 rows in Data grid, when user scroll the grid
im populating the data in the combo box using dataset tables...with some table name..now
I'm populating my select box using the query shown below: <%=f.select :acoustic, options_for_select(User.includes(:roles).where( :roles
I am populating the list view with data I am getting from a JSON
I am populating a listbox with a large list. The user then has the
populating table with initial data: additionsTable.setModel(new AdditionalDocsTableModel(addDocuments)); constructor of the model: public AdditionalDocsTableModel(List<MyDocument> docs)
I'm populating a drop down with JSON data from getJSON. Populating is working fine,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.