8.18 Programmatically Adding Items to a TreeView Control

 <  Day Day Up  >  

8.18 Programmatically Adding Items to a TreeView Control

You want to dynamically add hierarchical items to a TreeView control using the TreeView control's methods .


Technique

The data type for a single node in a tree is named TreeNode . To add a new root node to a TreeView , call the Add method defined in the Nodes collection passing a TreeNode object:

 
 TreeNode[] roots = new TreeNode[2]; for( int i = 0; i < 2; i++ ) {     roots[i] = new TreeNode( "Root " + i.ToString() );     treeView1.Nodes.Add( roots[i] ); } 

To add a child item, call the Add method defined in the Nodes collection of the parent TreeNode object:

 
 private void BuildChildren( int count, TreeNode parent ) {     for( int i = 0; i < count; i++ )     {         parent.Nodes.Add("Child " + (i+1).ToString() );     } } 

Comments

Although the TreeNode editor is a quick way of creating an initial set of TreeView items, your application is more likely going to be dynamic in nature, thus facilitating the need to dynamically add TreeNode items during application runtime. Unlike most other collection-based controls, the TreeView control needs to maintain a couple of object-accessor properties for each item within the control to allow hierarchical navigation. These properties within each TreeNode object consist of the Nodes collection, which contains all child items of the node, and a Parent object, which allows you to navigate up one level in the tree. Furthermore, because each tree view item contains a Nodes collection, you add new items to the TreeView control using methods defined within the TreeNode objects themselves rather than the TreeView control as is the case with most other collection-based controls. The only exception is with root items.

Because the root item within the tree does not have a parent, you create a root item using the Nodes property of the TreeView control. Any time you add an item to this collection, a new tree root is created. To add a child item using an already created root item as the parent, use an indexer of the tree view's Nodes collection and call the appropriate addition method on the object that is returned from the indexer.

One thing you need to keep in mind when dynamically building a tree is performance issues. Although it might seem easy to simply search for the corresponding location to place a new child item each time you need to add an item, the time spent searching can dramatically grow as more items are added. You should therefore create internal TreeNode member variables within your form class that allow you to perform an immediate addition to the tree at a specified location rather than continuously search each time. If the data you receive is random and periodic, then this procedure might not be feasible , but if, for instance, you are populating a tree control using a directory traversal routine, you should maintain some sort of state so you know that the next file should be inserted underneath the last folder that was added. In other words, if you find yourself enumerating through several Nodes collections to insert a single item each time, look for ways to decrease the amount of time searching to increase runtime performance.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net