How to expand first level children only of Treeview
Asked Answered
I

7

10

I want to show all children of the first level on the treeview by default. And then expand all children of those on click.

Ideally answered 27/12, 2011 at 19:58 Comment(1)
Iterate the nodes and call their Expand() method. "those on click" is way too vague.Phenetidine
W
14

Try:

foreach (TreeNode tn in treeView1.Nodes) {
  tn.Expand();
}

When adding nodes during runtime, you can just check the level and expand, if needed:

private void ShouldAutoExpand(TreeNode tn) {
  if (tn.Level == 0)
    tn.Expand();
}

There is no NodeAdded event you can hook into to check that automatically. You would have to determine yourself whether or not a node should be expanded "by default".

Update:

From your comment, it seems like you want to have all level 0 nodes expanded, but then expand all child nodes of level 1 when you expand them.

Try subscribing to the BeforeExpand event with this code:

private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e) {
  treeView1.BeforeExpand -= treeView1_BeforeExpand;
  if (e.Node.Level == 1) {
    e.Node.ExpandAll();
  }
  treeView1.BeforeExpand += treeView1_BeforeExpand;
}
Welladvised answered 27/12, 2011 at 20:3 Comment(3)
the first part shows first level children as i would like. But when i click on any of them the heir children are not fully expanded.Ideally
@Ideally Updated code. It sounds like you basically want all nodes expanded automatically "except" level 1 nodes. Sound right?Welladvised
thats the one, i was trying with onmouseclick which was mess. thanksIdeally
D
1

you can try something like this.. you will have to change the example to fit your own code since you neglected to paste any code that you have or attempted

private void addChildNode_Click(object sender, EventArgs e) 
{
  var childNode = textBox1.Text.Trim();
  if (!string.IsNullOrEmpty(childNode)) {
    TreeNode parentNode = treeView2.SelectedNode ?? treeView2.Nodes[0];
    if (parentNode != null) {
      parentNode.Nodes.Add(childNode);
      treeView2.ExpandAll();
    }
  }
}
Demulsify answered 27/12, 2011 at 20:5 Comment(0)
W
1

if you want a recursive, try this:

void expAll(TreeNode node)
{
   node.Expand();
   foreach(TreeNode i in node.Nodes)
   {
       expAll(i);
   }
}
Wheelhouse answered 27/12, 2011 at 20:15 Comment(0)
J
1
private TreeNode ExpandUptoLevel(TreeNode tn,int level)
    {
        if (level != 0)
        {
             level --;
             tn.Nodes[0].Expand();
             return ExpandUptoLevel(tn.FirstNode, level);
        }                               
        return tn;  
     }
Janettjanetta answered 14/8, 2013 at 12:2 Comment(0)
H
0

To expand all nodes in a tree to a level the above code does not work. Just add a check to read and compare the actual node level to the level that you wish to expand to. Here's a code snippet.

    private void ExpandUptoLevel(TreeNode tn, int level)
    {
        if (level >= tn.Level)
        { 
            tn.Expand();
            foreach (TreeNode i in tn.Nodes)
            {
                ExpandUptoLevel(i,level);
            }
        }
    }
Heathcote answered 13/7, 2015 at 13:52 Comment(1)
Response to Ravindra Sinare post - not to post immediately above.Heathcote
E
0

Only to open the first nodes:

for (int i = 0;  i< treeView1.Nodes.Count; i++)
        {
            treeView1.Nodes[i].Expand();
        }
Escolar answered 24/1, 2017 at 10:9 Comment(1)
While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations!Thessalonians
D
0

I found this worked ok for expanding the nodes relevant to the current selected node:

private void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
    if (TreeView1.SelectedNode.Depth != 0)
    {
        foreach (TreeNode node in TreeView1.SelectedNode.Parent.ChildNodes)
            node.Collapse();
    }

    TreeView1.SelectedNode.Expand();
}
Dedal answered 28/8 at 1:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.