Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically click on node in treeview?

I would really need to programmatically click on all nodes in the collection, but I cannot see the way how to do it. I end up with trying to call Node_Click event but I don't know how to use arguments.

foreach (TreeNode node in treeView1.Nodes)
{
    //here I would need to "click" on each node
}

EDITED: I need to raise TreeNode_After select. It's because treeview represents DB structure and if you click on node, it may or may not have childs (depends on what DB retrieves). This cycle should serve as ExpandAll.

like image 815
Petr Avatar asked Sep 22 '26 00:09

Petr


1 Answers

To cause every node in the tree to get selected, do this:

 void SelectAllNodes(TreeNodeCollection tnc)
 {
     foreach(TreeNode t in tnc)
     {
        treeView1.SelectedNode = t;
        SelectAllNodes(t.Nodes);
     }
 }

EDIT:
It's also worth noting that your code:

 foreach (TreeNode node in treeView1.Nodes)
 {
      //here I would need to "click" on each node
 }

Won't fire on every node in the tree, it will only return the nodes on the uppermost level. So if any of them have child nodes, they wont be seen by your foreach above. If you want to get EVERY node in the whole tree, you will need to recurse through them, like I did in my example above.

like image 136
Neil N Avatar answered Sep 23 '26 14:09

Neil N