0
votes

I have a treeview node that I would like to expand just only one special node. I know that by writing treeView1.ExpandAll() it will expand all the nodes and sub-nodes. But what I mean is something like this:

enter image description here

I tried also writhing this command but it has no influence in the tree(!):

TreeNode lastNode = treeView1.Nodes[0].Nodes[treeView1.Nodes.Count - 1];
lastNode.Expand();
2

2 Answers

0
votes

That's because your last node is the really last in your tree maybe - the one without text underneath "Manipulieren (data)". What you need is the Find Method of the treeview Nodes Collection.

treeview.Nodes.Find("KeyOfTheNode",includeChildren)

where includeChildren tells the method if it should search top-level only or include subnodes.

BUT: you need to add the nodes with a key value! like this:

treeview.Nodes.Add("KeyOfTheNode", "TextOfTheNode")

and keep in mind: Find(string key, bool searchAllChildren)returns an array of TreeNodes.

0
votes

First, make sure you manipulate the correct node. In your example treeView1.Nodes.Count is 1 (the root node), so

TreeNode lastNode = treeView1.Nodes[0].Nodes[treeView1.Nodes.Count - 1];

translates to

TreeNode lastNode = treeView1.Nodes[0].Nodes[0];

which is not your intention.

The node "actions" can be retrieved with

TreeNode lastNode = treeView1.Nodes[0].LastNode;

Second, calling Expand is not enough if some of the parent nodes is not expanded. You need to either include EnsureVisible call like this

lastNode.Expand();
lastNode.EnsureVisible();

or manually expand the node and all its parents like this (you could also make a function)

for (var node = lastNode; node != null; node = node.Parent)
    node.Expand();