mardi 19 novembre 2019

In C#, how to set all tree nodes checked true on loaded treeview

I have a TreeView in a WindowForm build in C#. In that treeview, files are being loaded successfully. I've also implemented checkboxes, showing in front of each tree node.

Now, I want all checkboxes to be checked true by default after all directories are loaded in treeview. I tried the following code but no luck. It just checks true only the root node, not all the sub-nodes. The first method btnDirectoryPath_Click(object sender, EventArgs e) is for button click which selects the path of the directory being loaded. From here, I am calling setAllCheckedTrue(TreeView treeView1)

private void btnDirectoryPath_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.SelectedPath = txtDirectoryPath.Text;
            DialogResult drResult = folderBrowserDialog1.ShowDialog();
            if (drResult == System.Windows.Forms.DialogResult.OK)
            {
                txtDirectoryPath.Text = folderBrowserDialog1.SelectedPath;

                // Setting Inital Value of Progress Bar
                progressBar1.Value = 0;
                // Clear All Nodes if Already Exists
                treeView1.Nodes.Clear();
                toolTip1.ShowAlways = true;
                if (txtDirectoryPath.Text != "" && Directory.Exists(txtDirectoryPath.Text))
                {
                    //Loading all directories and sub directories and files
                    LoadDirectory(txtDirectoryPath.Text);
                    //setting all checkboxes true by default on loading.
                    setAllCheckedTrue(treeView1);
                }
                else
                    MessageBox.Show("Select Directory!!");
            }
        }

private void setAllCheckedTrue(TreeView treeView1)
        {
            foreach(TreeNode treeNode in treeView1.Nodes)
            {
                treeNode.Checked = true;
            }
        }

Following is snapshot of my treeview -

Snapshot of my treeview

Another doubt is, How to handle on treeview load event? Is there any specific thing in c# to detect treeview load event?

I am a beginner to C#, Please help me out if possible. Thanks!




Aucun commentaire:

Enregistrer un commentaire