开发者

what might be causing this to fill this treeview multiple times?

I found this code online. It works, but for some reason it loads the file directory twice.

namespace DockSample.Controls
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using System.Drawing;
    using Microsoft.VisualBasic.FileIO;
    using DockSample;



    //TODO: Add options for filtering by robot
       public class SolutionExplorer : TreeView
        {

            public SolutionExplorer()
            {
                this.BeforeExpand += customBeforeExpand;
                this.Nodes.Clear();
                CreateTree(this);
            }

            private bool CreateTree(TreeView treeView)
            {

                bool returnValue = false;

                try
                {
                    // Create Desktop
                    TreeNode desktop = new TreeNode();
                  //  desktop.Text = "Desktop";
                  //  desktop.Tag = "Desktop";
                  //  desktop.Nodes.Add("");
                  //  treeView.Nodes.Add(desktop);
                    // Get driveInfo
                    foreach (DriveInfo drv in DriveInfo.GetDrives())
                    {

                        TreeNode fChild = new TreeNode();
                        if (drv.DriveType == DriveType.CDRom)
                        {
                            fChild.ImageIndex = 1;
                            fChild.SelectedImageIndex = 1;
                        }
                        else if (drv.DriveType == DriveType.Fixed)
                        {
                            fChild.ImageIndex = 0;
                            fChild.SelectedImageIndex = 0;
                        }
                        fChild.Text = drv.Name;
                        fChild.Nodes.Add("");
                        treeView.Nodes.Add(fChild);
                        returnValue = true;
                    }

                }
                catch 
                {
                    returnValue = false;
                }
                return returnValue;

            }

            /* Method :EnumerateDirectory
            * Author : Chandana Subasinghe
            * Date : 10/03/2006
            * Discription : This is use to Enumerate directories and files
            * 
            */
            public TreeNode EnumerateDirectory(TreeNode parentNode, List<string> thisFilter)
            {

                try
                {
                    DirectoryInfo rootDir;

                    // To fill Desktop
                    Char[] arr = { '\\' };
                    string[] nameList = parentNode.FullPath.Split(arr);
                    string path = "";

                    if (nameList.GetValue(0).ToString() == "Desktop")
                    {
                        path = SpecialDirectories.Desktop + "\\";

                        for (int i = 1; i < nameList.Length; i++)
                        {
                            path = path + nameList[i] + "\\";
                        }

                        rootDir = new DirectoryInfo(path);
                    }
                    // for other Directories
                    else
                    {

                        rootDir = new DirectoryInfo(parentNode.FullPath + "\\");
                    }

                    parentNode.Nodes[0].Remove();
                    foreach (DirectoryInfo dir in rootDir.GetDirectories())
                    {

                        TreeNode node = new TreeNode();
                        node.Text = dir.Name;
                        node.Nodes.Add("");
                        parentNode.Nodes.Add(node);
                    }
                    //Fill files
                    foreach (FileInfo file in rootDir.GetFiles())
                    {
                        if (isValidFilter(getExtention(file.Name)))
                        {
                            TreeNode node = new TreeNode();
                            node.Text = file.Name;
                            node.ImageIndex = 2;
                            node.SelectedImageIndex = 2;
                            parentNode.Nodes.Add(node);
                        }
                    }



                }

                catch 
                {
                }

                return parentNode;
            }
            private bool isValidFilter(string ext)
            {
                bool result = false;
                foreach(string s in filter)
                {
                    if (ext == s)
                        result =  true;

                }

                return result;
            }
            private string getExtention(string filename)
            {
                return filename.Substring(filename.IndexOf("."));
            }
            private void customBeforeExpand(object sender, TreeViewCancelEventArgs e)
            {
                if (e.Node.Nodes[0].T开发者_如何学Cext == "")
                {
                    TreeNode node = this.EnumerateDirectory(e.Node,filter);
                }
            }
            private List<string> filter = new List<string>();
            public List<string> Filter
            {
                get { return filter; }
                set { filter = value; }
            }
            private void InitializeComponent()
            {
                this.SuspendLayout();
                this.ResumeLayout(false);

            }
        }

}


The constructor of your control runs at both design time and run time. So as soon as you drop the control on the form, it will fill the tree view. Problem is, the nodes will be serialized to InitializeComponent(). Take a look at the Designer.cs file for your form, you'll find them back there. When you run the form, the constructor runs again, doubling the list.

You need to prevent the constructor from adding the nodes at design time. That's a bit difficult to do, you'd normally use the DesignMode property but it isn't set to true yet in the constructor. Do it like this instead:

protected override void OnHandleCreated(EventArgs e) {
    base.OnHandleCreated(e);
    if (!DesignMode && treeView.Nodes.Count == 0) {
        CreateTree(this);
    }
}

Or do it explicitly by adding a public method that you call in the form's constructor or OnLoad method. Which is rather wise, you might want to catch exceptions. Always likely when you tinker with the file system.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜