0

Actually My Question is: I have a treeview that displaying the logical drivers with checkboxes, If I select C: drive It should be select all the folders and subfolders. If I Deselect the C: drive It should deselect all the folders and subfolders within subfolders...

My Code :

    private void Form2_Load(object sender, EventArgs e)
    {
        treeView1.CheckBoxes = true;
        foreach (TreeNode node in treeView1.Nodes)
        {
            node.Checked = true;
        }

        string[] drives = Environment.GetLogicalDrives();

        foreach (string drive in drives)
        {
           // treeView1.Nodes[0].Nodes[1].Checked = true;
            DriveInfo di = new DriveInfo(drive);
            int driveImage;

            switch (di.DriveType)   
            {
                case DriveType.CDRom:
                    driveImage = 3;
                    break;
                case DriveType.Network:
                    driveImage = 6;
                    break;
                case DriveType.NoRootDirectory:
                    driveImage = 8;
                    break;
                case DriveType.Unknown:
                    driveImage = 8;
                    break;
                default:
                    driveImage = 2;
                    break;
            }

            TreeNode node = new TreeNode(drive.Substring(0, 1), driveImage, driveImage);
            node.Tag = drive;


            if (di.IsReady == true)
                node.Nodes.Add("...");

            treeView1.Nodes.Add(node);


        }

        foreach (TreeNode node in treeView1.Nodes)
        {
            node.Checked = true;
        }
    }


    private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
    {


        if (e.Node.Nodes.Count > 0)
        {
            if (e.Node.Nodes[0].Text == "..." && e.Node.Nodes[0].Tag == null)
            {
                e.Node.Nodes.Clear();


                string[] dirs = Directory.GetDirectories(e.Node.Tag.ToString());

                foreach (string dir in dirs)

                {
                    DirectoryInfo di = new DirectoryInfo(dir);
                    TreeNode node = new TreeNode(di.Name, 0, 1);
                    node.Checked = true;

                    try
                    {
                        node.Tag = dir;  


                        if (di.GetDirectories().Count() > 0)
                            node.Nodes.Add(null, "...", 0, 0);
                    }
                    catch (UnauthorizedAccessException)
                    {
                        node.ImageIndex = 12;
                        node.SelectedImageIndex = 12;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "DirectoryLister", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
                    }
                    finally
                    {
                        e.Node.Nodes.Add(node);
                    }
                }

            }
        }

    }

    private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
    {
        foreach (TreeNode childNode in e.Node.Nodes)
        {
            childNode.Checked = e.Node.Checked;

        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        foreach (TreeNode node in treeView1.Nodes)
        {
            node.Checked = true;

        }

     }

    private void button2_Click(object sender, EventArgs e)
    {
        foreach (TreeNode node in treeView1.Nodes)
        {
             node.Checked = false;             
        }
    }

    private void button3_Click(object sender, EventArgs e)
    {
        Form3 f3 = new Form3();
        f3.ShowDialog();

    }


}

}

Santosh Kokatnur
  • 357
  • 3
  • 9
  • 19
  • Hello, Suppose i click on the C: drive and it will check all the folders and subfolders and i m using two buttons for that one is select all and second button is Deselect all. if i select a folder in C : Drive, the folder will checked and also subfolders, but my question is in C:Drive there are many other folder are there, those folder's subfolder also selecting.. – Santosh Kokatnur Dec 04 '15 at 07:38
  • Hm, I still don't understand. Why do you need buttons? As it is the child nodes get selected and delselected by checking or unchecking the node you click onn. You should __change the node creation__ to use the parent.Checked value: `node.Checked = e.Node.Checked; //true;` but other than that all works as you describe you want it to! Your `AfterCheck` event propagates the new state all the way down. Only the small correction above is needed, imo.. – TaW Dec 04 '15 at 18:46
  • Well, you didn't show the initialization code you use. It probably is missing something there. The way I wrote it it works just fine. I didn't use any of your button code as clicking the checkboxes is enough.. Did you add the little correction? – TaW Dec 05 '15 at 08:12
  • sam.codemeone@gmail.com it is my mail ID. Please help me out... – Santosh Kokatnur Dec 05 '15 at 08:38
  • Hm, the init code is fine as well. I have a hunch that you didn't [hook up](http://stackoverflow.com/questions/33275763/copy-datagridview-values-to-textbox/33276161?s=1|0.3903#33276161) the `treeView1_AfterCheck` event.. - Can you test if you ever enter that event code?? – TaW Dec 05 '15 at 09:03
  • @TaW Can I have your mail id? – Santosh Kokatnur Dec 07 '15 at 05:18
  • I'm sorry but I do not post my mail address here. I can help you best when you answer my questions. Is the AfterCheck event hooked up? (If you are not sure do follow the link!) – TaW Dec 07 '15 at 06:58
  • sam.codemeone@gmail.com, connect me! – Santosh Kokatnur Dec 07 '15 at 07:10
  • The blue link in the 2nd to last comment! – TaW Dec 07 '15 at 07:18

0 Answers0