vendredi 8 novembre 2019

In c# tree view, checkbox and Imagelist icon overlapping, How to fix this?

I am working on developing tree view in C# wherein directories and files are to be loaded. TreeView and directories are successfully loaded. I need to have checkboxes with every tree node and also I want to have corresponding icons (eg. for directory - a folder icon, and for files - a file icon). When I try to do that, checkboxes and icons overlaps. Following is some snap shots for that -

Please click to see image

Following is the Image List code in Form1.Designer.cs file:

        this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
        this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
        this.imageList1.Images.SetKeyName(0, "Folder_256x256.png");
        this.imageList1.Images.SetKeyName(1, "document-management-big.png");

Following is the Directory and files loading code in Form1.cs file:

        public void LoadDirectory(string Dir)
    {
        DirectoryInfo di = new DirectoryInfo(Dir);
        //MessageBox.Show(di.Name.ToString());
        //Setting ProgressBar Maximum Value
        progressBar1.Maximum = Directory.GetFiles(Dir, "*.*", SearchOption.AllDirectories).Length + Directory.GetDirectories(Dir, "*.*", SearchOption.AllDirectories).Length;
        TreeNode tds = treeView1.Nodes.Add(di.Name);
        tds.Tag = di.FullName;
        tds.StateImageIndex = 0; //Code for loading/linking with icon
        LoadFiles(Dir, tds);
        LoadSubDirectories(Dir, tds);
    }

    private void LoadSubDirectories(string dir, TreeNode td)
    {
        // Get all subdirectories
        string[] subdirectoryEntries = Directory.GetDirectories(dir);
        // Loop through them to see if they have any other subdirectories
        foreach (string subdirectory in subdirectoryEntries)
        {

            DirectoryInfo di = new DirectoryInfo(subdirectory);
            TreeNode tds = td.Nodes.Add(di.Name);
            tds.StateImageIndex = 0; //Code for loading/linking with icon
            tds.Tag = di.FullName;
            LoadFiles(subdirectory, tds);
            LoadSubDirectories(subdirectory, tds);
            UpdateProgress();

        }
    }

    private void LoadFiles(string dir, TreeNode td)
    {
        string[] Files = Directory.GetFiles(dir, "*.*");

        // Loop through them to see files
        foreach (string file in Files)
        {
            FileInfo fi = new FileInfo(file);
            TreeNode tds = td.Nodes.Add(fi.Name);
            tds.Tag = fi.FullName;
            tds.StateImageIndex = 1; //Code for loading/linking with icon
            UpdateProgress();
        }
    }

The checkbox is directly enabled in the treeView by writing code in Form1.Designer.cs file :

this.treeView1.CheckBoxes = true;

The problem is already explained - icons and checkboxes are overlapping. Please help me in this. Not getting any solution on internet as per my research.

Thanks in advance!




Aucun commentaire:

Enregistrer un commentaire