Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing all the nodes in TreeView Control

I have a TreeView Control with set of nodes and child nodes. For example:

ROOT has A,B,C.

A has a1, a2, a3 and then that a1, a2 also contains some nodes like x1, x2, x3 and so on. Like this many subnodes are there. I know it is possible to use loops with a for loop.

I just want to access all the nodes in TreeView control using one or two for loops.

Is there any algorithm for that or is there any other way?

One more question: Is it is possible to have the path of a tree node in an object or in a string using any library functions? For example:

string S = TreeView1.Nodes[i].Nodes[j].Nodes
like image 575
user162558 Avatar asked Sep 03 '09 09:09

user162558


People also ask

How do I get all child nodes of TreeView in VB net?

Solution: In a NodeSelectedEvent event, the event argument (e) holds the object of the selected node – Node. The selected node object have property called Items, it holds a list of child node object of the selected node. By iterating the Item object, you can get the information of each child node.

How many root nodes can a TreeView control have?

A typical tree structure has only one root node; however, you can add multiple root nodes to the TreeView control. The Nodes property can also be used to manage the root nodes in the tree programmatically. You can add, insert, remove, and retrieve TreeNode objects from the collection.

How do I get data from TreeView database?

Populating TreeView from database Inside the PopulateTreeView method, a loop is executed over the DataTable and if the ParentId is 0 i.e. the node is a parent node, a query is executed over the VehicleSubTypes table to populate the corresponding child nodes and again the PopulateTreeView method is called.

Which property in the TreeView control represent the node?

The key properties of the TreeView control are Nodes and SelectedNode. The Nodes property contains the list of top-level nodes in the tree view. The SelectedNode property sets the currently selected node.


3 Answers

Don't use nested loops, but go for an recursive solution like:

void ListNodes( TreeNode node )
{
  foreach( var subnode in node.Nodes )
  {
    ListNodes( subnode );
  }
  // Print out node
}

Call this function for your root node.

For your additional question: check the FullPath property.

like image 156
tanascius Avatar answered Oct 18 '22 13:10

tanascius


You can use a recursive function to traverse the whole tree:

private void Traverse(TreeNodeCollection nodes)
{
    foreach (TreeNode node in nodes)
    {
        Console.WriteLine("{0} -> {1}", node.Name, node.FullPath);
        Traverse(node.Nodes);
    }
}

You can then call this using:

Traverse(treeView.Nodes);

and it will walk the whole tree depth first (ie. going down as deep as it can before moving to the next sibling). Passing in the Nodes collection means that this code will deal with trees that have multiple root nodes.

The example code above will print out the name of the node as well as the full path of that node within the tree.

like image 8
adrianbanks Avatar answered Oct 18 '22 12:10

adrianbanks


I am not the biggest fan of recursion but it seems you must use it. I saw a clever example online mixing recursion with an iterator.

    private int GetLevels(TreeNodeCollection treeNodes)
    {
        int level = 0;
        foreach (TreeNode node in TreeTopDown(treeNodes))
        {
            int i = node.Level;
            if (i > level) level = i;
        }
        return level;
    }

    //TopDown Iterator 
    private IEnumerable<TreeNode> TreeTopDown(TreeNodeCollection treeNodes)
    {
        foreach (TreeNode node in treeNodes)
        {
            yield return node;
            foreach (TreeNode subNode in TreeTopDown(node.Nodes)) yield return subNode;               
        }
    }

    //BottomUp Iterator
    private IEnumerable<TreeNode> TreeBottomUp(TreeNodeCollection treeNodes)
    {
        foreach (TreeNode node in treeNodes)
        {
            foreach (TreeNode subNode in TreeBottomUp(node.Nodes)) yield return subNode;
            yield return node;
        }
    }
like image 5
noclayto Avatar answered Oct 18 '22 13:10

noclayto