Listing all the child nodes of the parent node in a treeview control in Visual C #

I have a treeview control and it contains one parent node and several child nodes of this parent. Is there a way to get an array or list of all the child nodes of the primary parent? those. get all nodes from treeview.nodes [0] or the first parent node.

+7
source share
3 answers

You can add to the list recursively:

public void AddChildren(List<TreeNode> Nodes, TreeNode Node) { foreach (TreeNode thisNode in Node.Nodes) { Nodes.Add(thisNode); AddChildren(Nodes, thisNode); } } 

Then call this procedure in the root of the node:

 List<TreeNode> Nodes = new List<TreeNode>(); AddChildren(Nodes, treeView1.Nodes[0]); 
+6
source
 public IEnumerable<TreeNode> GetChildren(TreeNode Parent) { return Parent.Nodes.Cast<TreeNode>().Concat( Parent.Nodes.Cast<TreeNode>().SelectMany(GetChildren)); } 
+9
source

You can do something like this .. to get all the nodes in the tree structure.

  private void PrintRecursive(TreeNode treeNode) { // Print the node. System.Diagnostics.Debug.WriteLine(treeNode.Text); MessageBox.Show(treeNode.Text); // Print each node recursively. foreach (TreeNode tn in treeNode.Nodes) { PrintRecursive(tn); } } // Call the procedure using the TreeView. private void CallRecursive(TreeView treeView) { // Print each node recursively. TreeNodeCollection nodes = treeView.Nodes; foreach (TreeNode n in nodes) { PrintRecursive(n); } } 

You would take alook from this link.

http://msdn.microsoft.com/en-us/library/wwc698z7.aspx

-2
source

All Articles