Programmatically click on a node in the form of a tree?

I really would have to programmatically click on all the nodes in the collection, but I cannot figure out how to do this. I am trying to raise a Node_Click event, but don’t know how to use the arguments.

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

Edited: I need to raise the TreeNode_After select. This is because the tree structure represents the database structure, and if you click on a node, it may or may not have children (depending on which database it retrieves). This loop should serve ExpandAll.

+3
source share
4 answers

To make each node in the selected tree select, do the following:

 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
 }

node , . , , . node , , .

+3

"" , node "":

foreach (TreeNode node in treeView1.Nodes)
{
   node_click(node, null)
}

protected void node_click(object sender, System.EventArgs e )
{
    //...Your code here

}
+1

, ?

        foreach (TreeNode node in this.treeView1.Nodes)
        {
            this.treeView1.SelectedNode = node;
        }
+1

, TreeView.NodeMouseClick, ? , foreach :

foreach (TreeNode node in treeView1.Nodes)
{
    treeView1_NodeMouseClick(node, null);
}

, ,

treeView1.NodeMouseClick += new TreeNodeMouseClickEventHandler(treeView1_NodeMouseClick);

sloppy , :

public void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
    TreeNode node = sender as TreeNode;
    if (node != null)
        MessageBox.Show(node.Text);
}

null TreeNodeMouseClickEventArgs, .

:

Looks like you just have to just call the AfterSelect (...) method by directly calling when your user clicks the Expand All button. So, if I guess correctly about your architecture, you want to add an AfterSelect call to the click handler of your Expand All button

0
source

Source: https://habr.com/ru/post/1721511/


All Articles