Click here to Skip to main content
15,889,315 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
By the example i have took logical drivers as a TreeView, and i took two buttons in my application, SELECT ALL and Deselect all those two buttons.


When i execute the application initially, the drives in checked State, If I want to Deselect the drives all checkboxes are uncheck, thats ok! But, subfolders can't uncheck, Still it remains in uncheck state.

C#
Code I want... Please help me out...
Posted

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace DirectoryLister
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        
        public bool Checked { get; set; }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {

        }

        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;
                 treeView1.SelectedNode = null;             
            }
        }

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

        
    }
}
 
Share this answer
 
v3
Comments
BillWoodruff 8-Dec-15 7:33am    
If this really is a "solution" to your question, that's okay according to current CP rules, but you may get down-voted for dumping a bunch of un-formatted code.

If this code is meant to help other people understand your question, then please delete this "solution" and edit your original post to include the code (and format it !).

It is always better to post selected code that's directly related to whatever problem you are having.
Santosh Kokatnur 8-Dec-15 7:48am    
Actually, Its not a full solution, In this select is perfectly working but if i try to deselect the drives means only folder can deselect but not subfolder, subfolders remains checked... please tell me the code...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900