zsharp
zsharp

Reputation: 13756

How to expand first level children only of Treeview

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

Upvotes: 10

Views: 37733

Answers (7)

Alan Warren
Alan Warren

Reputation: 11

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();
}

Upvotes: 0

LarsTech
LarsTech

Reputation: 81610

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;
}

Upvotes: 14

ardem
ardem

Reputation: 21

Only to open the first nodes:

for (int i = 0;  i< treeView1.Nodes.Count; i++)
        {
            treeView1.Nodes[i].Expand();
        }

Upvotes: 0

Peter Bauer
Peter Bauer

Reputation: 1

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);
            }
        }
    }

Upvotes: 0

Ravindra Sinare
Ravindra Sinare

Reputation: 11

private TreeNode ExpandUptoLevel(TreeNode tn,int level)
    {
        if (level != 0)
        {
             level --;
             tn.Nodes[0].Expand();
             return ExpandUptoLevel(tn.FirstNode, level);
        }                               
        return tn;  
     }

Upvotes: 1

Gustavo Maciel
Gustavo Maciel

Reputation: 652

if you want a recursive, try this:

void expAll(TreeNode node)
{
   node.Expand();
   foreach(TreeNode i in node.Nodes)
   {
       expAll(i);
   }
}

Upvotes: 1

MethodMan
MethodMan

Reputation: 18843

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();
    }
  }
}

Upvotes: 1

Related Questions