mardi 2 août 2016

Get all child nodes list and check or uncheck them

I'm looking for this kind of solutions I got it from different-2 solution and mix it up them make a complete solution I hope this will someone who are looking for this kind of solution.

private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
    if (e.Action != TreeViewAction.Unknown)
    {
        tempDirs.Add(e.Node.Tag.ToString());
        treeView_AddChecked(e.Node);
        if (e.Node.Nodes.Count > 0)
        {
            this.CheckAllChildNodes(e.Node, e.Node.Checked);
        }
    }
}

this the function that call recursively to check its child nodes.

private void CheckAllChildNodes(TreeNode treeNode, bool nodeChecked)
{
    foreach (TreeNode node in treeNode.Nodes)
    {
        node.Checked = nodeChecked;
        if (node.Nodes.Count > 0)
        {
            this.CheckAllChildNodes(node, nodeChecked);
        }
    }
}

and that gonna show all selected child node on expand it.

private void treeView1_AfterExpand(object sender, TreeViewEventArgs e)
{
    if(e.Node.Checked)
    {
        CheckAllChildNodes(e.Node, e.Node.Checked);
    }
}

and this functionality will get you the list of all the directories.

private void treeView_AddChecked(TreeNode Node)
    {
        Node.Nodes.Clear();
        string[] dirs = Directory.GetDirectories(Node.Tag.ToString());

        foreach (string dir in dirs)
        {
            tempDirs.Add(dir);
            DirectoryInfo di = new DirectoryInfo(dir);
            TreeNode node = new TreeNode(di.Name, 0, 1);
            node.Tag = dir;
            string[] subDirs = Directory.GetDirectories(node.Tag.ToString());
            if(subDirs.Count() > 0)
            {
                treeView_AddChecked(node);
            }
        }
    }




Aucun commentaire:

Enregistrer un commentaire