Kevin Babb
Kevin Babb

Reputation: 111

treeview how to expand a fullpath

I need to expand my treeview based on a fullpath in c#

My tree view has 2 nodes which are collapsed and I want to expand Node A to number 3 so I have the fullpath of node A\1\2\3.

How can I step through and open each node based of the fullpath? Also the length of the fullpath may change, so i may need to open node be to level 6. So it needs to be done based on the fullpath. Any help would be great.

Node A
       1
         2
          3
Node B
1
 2
  3
   4 5 6

This is what I've tried:

TreeNode [] n= treeView1.Nodes.Find(search, true);

if (n.Length > 0)
  found = true;
treeView1.Nodes[t].Collapse();
foreach (TreeNode p in n) {
  string[] a = p.FullPath.Split('\\');
  foreach (string b in a) {
    treeView1.SelectedNode = treeView1.Nodes[b];
    treeView1.SelectedNode.Expand();

Upvotes: 3

Views: 11201

Answers (4)

Antun Matic
Antun Matic

Reputation: 56

I'm sorry for not commenting on above answer given by S3ddi9 which is CORRECT. I'm only adding something.

So the answer given by S3ddi9

    ...
    string path = @"A\1\2\";

    var path_list = path.Split('\\').ToList();
    foreach (TreeNode node in treeView1.Nodes)
        if (node.Text == path_list[0])
            ExpandMyLitleBoys(node, path_list);
}

private void ExpandMyLitleBoys(TreeNode node, List<string> path)
{
    path.RemoveAt(0);

    node.Expand();

    if (path.Count == 0)
        return;

    foreach (TreeNode mynode in node.Nodes)
        if (mynode.Text == path[0])
          {  
             ExpandMyLitleBoys(mynode, path); //recursive call
             break;  //this was missing in earlier answer
          } 

}

Does work, BUT you must add a BREAK (I marked it), because if the for loop doesn't finish, return; won't return you to your main function and it will throw you an exception because path[0] is null.

Upvotes: 4

Kevbo
Kevbo

Reputation: 943

I wrote a little simpler routine that works great. no recursion at all... This assumes your path is a full file path like... "C:\program files\myapp" and when you add your nodes you set the node Key equal to the folder name

string[] strFolders = strPath.Split('\'));
System.Windows.Forms.TreeNode CurrentNode = null;
System.Windows.Forms.TreeNode[] FoundNodes = null;
foreach (string folder in strFolders) {
	if (!folder.Contains(":")) {
		if (CurrentNode == null) {
			FoundNodes = treeFolders.Nodes.Find(folder, false);
		} else {
			FoundNodes = CurrentNode.Nodes.Find(folder, false);
		}
		if (FoundNodes.Length > 0) {
			CurrentNode = FoundNodes[0];
			CurrentNode.Expand();
		} else {
			//no folder found.  cant continue
			break;
		}
	}
}
if (CurrentNode != null) {
	treeFolders.SelectedNode = CurrentNode;
}

Upvotes: 0

S3ddi9
S3ddi9

Reputation: 2151

I hope this will be more simpler, for Treeviews the best approach is to use recursive methods

   ...
        string path = @"A\1\2\";

        var path_list = path.Split('\\').ToList();
        foreach (TreeNode node in treeView1.Nodes)
            if (node.Text == path_list[0])
                ExpandMyLitleBoys(node, path_list);



    }

    private void ExpandMyLitleBoys(TreeNode node, List<string> path)
    {
        path.RemoveAt(0);

        node.Expand();

        if (path.Count == 0)
            return;

        foreach (TreeNode mynode in node.Nodes)
            if (mynode.Text == path[0])
                ExpandMyLitleBoys(mynode, path); //recursive call


    }

this does the work perfectly

Upvotes: 1

Kevin Babb
Kevin Babb

Reputation: 111

Cracked it!!

 TreeNode[] n = treeView1.Nodes.Find(search, true);
            if (n.Length > 0)
                found = true;
            treeView1.Nodes[t].Collapse();

            foreach (TreeNode p in n)
            {
                i = 0;
                string[] a = p.FullPath.Split('\\');
                foreach (string b in a)
                {

                    if (i == 0)
                    {

                        treeView1.SelectedNode = treeView1.Nodes[b];
                        current = treeView1.Nodes[b];
                        treeView1.SelectedNode.Expand();
                        i = 1;
                    }
                    else
                    {
                        treeView1.SelectedNode = current.Nodes[b];
                        current = current.Nodes[b];
                        treeView1.SelectedNode.Expand();


                        if (b.ToUpper().Contains(search))
                        {
                            treeView1.SelectedNode.BackColor = System.Drawing.Color.Red;
                        }

Upvotes: 0

Related Questions