private void PrintRecursive(TreeNode treeNode)
{
foreach (TreeNode tn in treeNode.Nodes)
{
PrintRecursive(tn);
}
}
I get the error: Foreach cannot operate on a method group. Did you intend to invoke the 'method group'?
The problem here is that Nodes
is a method but you use as property :)
So this line of code
foreach (TreeNode tn in treeNode.Nodes)
should be
foreach (TreeNode tn in treeNode.Nodes())
Assuming you're using the packaged TreeView control, shouldn't it be ChildNodes
?:
foreach (TreeNode node in treeNode.ChildNodes) ...
TreeView.Nodes gives a collection of TreeNode objects that represents the root nodes in the TreeView control.
To access the child nodes of a root node, use the ChildNodes property of the node.
e.g. using for loop
void PrintRecursive(TreeNode node)
{
for(int i=0; i <node.ChildNodes.Count; i++)
{
PrintRecursive(node.ChildNodes[i]);
}
}
or using foreach
void PrintRecursive(TreeNode node)
{
foreach(TreeNode node in node.ChildNodes)
{
PrintRecursive(node);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With