2

I'm trying to add nodes dynamically during run-time to an existing tree-view emulating windows explorer. I have code that works, but because of the amount of recursion that takes place it takes 2-3 minutes to check for all of the files on the c: drive and create the tree.

What I would like to do instead is something like this:

 -NodeClickEvent-
if has children { do nothing }
else { add children and grandchildren to selected node }

This is so that it does not have to load the entire tree, but instead loads a couple layers at a time on a per-click basis.

TK421
  • 801
  • 5
  • 16
  • 24
  • You can do that pretty much the way you described it. – alex Jun 21 '13 at 20:31
  • 2
    You could try avoiding recursion by using a simple loop and a stack. [Here's an example](http://stackoverflow.com/a/2527714/119477) where a directory structure is copied without recursion. Instead of copy you'd create nodes on your tree. This may or may not improve perf but you'll at least avoid a `StackOverflowException` – Conrad Frix Jun 21 '13 at 22:15

3 Answers3

2

IF you are not very familiar wtih tree view , then you can first take this tutorial http://www.dotnetperls.com/treeview and this tutorial describes how to use treelist.

And these following links explain that what you are looking for

http://msdn.microsoft.com/en-us/library/aa984278%28VS.71%29.aspx

http://msdn.microsoft.com/en-us/library/aa645739%28v=vs.71%29.aspx

Kas
  • 3,747
  • 5
  • 29
  • 56
1

http://www.codeproject.com/Articles/26288/Simplifying-the-WPF-TreeView-by-Using-the-ViewMode

Look at demo 2 in the same article

when creating a treeview item, attach a dummy object On expansion of each treeview item, delete the dummy child if it is in and add actual children if at all it has.

Its very clearly explained in the above quoted article

RADKrish
  • 51
  • 7
  • Much thanks for the response! I am working with Windows Forms though, and it appears the above article is for WPF. – TK421 Jun 21 '13 at 21:18
0

Thanks for all the input; I think I finally figured out how to do what I was looking for here: An unhandled exception of type 'System.NullReferenceException' occurring when clicking TreeNode

My Code is:

public Form1()
        {
            InitializeComponent();
            this.treeView1.NodeMouseClick += new TreeNodeMouseClickEventHandler(this.treeView1_NodeMouseClick);
            PopulateTreeView();
        }
 private void PopulateTreeView()
   {
       TreeNode rootNode;

       DirectoryInfo info = new DirectoryInfo(@"c:\\");
       if (info.Exists)
       {
           rootNode = new TreeNode(info.Name);
           rootNode.Tag = info;
           GetDirectories(info.GetDirectories(), rootNode);
           treeView1.Nodes.Add(rootNode);
       }
   }

   private void GetDirectories(DirectoryInfo[] subDirs, TreeNode nodeToAddTo)
   {
       TreeNode aNode;
       //DirectoryInfo[] subSubDirs;
       foreach (DirectoryInfo subDir in subDirs)
       {
           aNode = new TreeNode(subDir.Name, 0, 0);
           aNode.Tag = subDir;
           aNode.ImageKey = "folder";
           /*  try
             {
                   subSubDirs = subDir.GetDirectories();
                   if (subSubDirs.Length != 0)
                   {
                       GetDirectories2(subSubDirs, aNode);
                   }
             }
             catch (System.UnauthorizedAccessException)
             {
                 subSubDirs = new DirectoryInfo[0];
             }*/
           nodeToAddTo.Nodes.Add(aNode);
       }
   }


   void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
   {               
       try
       {
           DirectoryInfo d = new DirectoryInfo(@e.Node.FullPath);
           if (e.Node.Nodes.Count > 0) { /*Do Nothing.*/ } else { GetDirectories(d.GetDirectories(), e.Node); e.Node.Expand(); }

           TreeNode newSelected = e.Node;
           listView1.Items.Clear();
           DirectoryInfo nodeDirInfo = (DirectoryInfo)newSelected.Tag;
           ListViewItem.ListViewSubItem[] subItems;

           ListViewItem item = null;

           foreach (DirectoryInfo dir in nodeDirInfo.GetDirectories())
           {
               item = new ListViewItem(dir.Name, 0);
               subItems = new ListViewItem.ListViewSubItem[]
                {new ListViewItem.ListViewSubItem(item, "Directory"), 
                 new ListViewItem.ListViewSubItem(item, 
                    dir.LastAccessTime.ToShortDateString())};
               item.SubItems.AddRange(subItems);
               listView1.Items.Add(item);
           }
           foreach (FileInfo file in nodeDirInfo.GetFiles())
           {
               item = new ListViewItem(file.Name, 1);
               subItems = new ListViewItem.ListViewSubItem[]
                { new ListViewItem.ListViewSubItem(item, "File"), 
                 new ListViewItem.ListViewSubItem(item, 
                    file.LastAccessTime.ToShortDateString())};

               item.SubItems.AddRange(subItems);
               listView1.Items.Add(item);
           }

           listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
       }
       catch (Exception ex)
       {

            if (ex is System.NullReferenceException || ex is System.UnauthorizedAccessException)
            {
                // Do Nothing.
            }

       }



   }`
Community
  • 1
  • 1
TK421
  • 801
  • 5
  • 16
  • 24