Add Treeview Node by path

0

I'm trying to add a TreeView Node to the path, for example:

And the code I'm trying is like this:

public void AddParent(string path, string node)
{
    TreeNode parentNode = treeView1.Nodes[path];
    if (parentNode != null)
    {
        parentNode.Nodes.Add(node);
    }
}

Ex path: Node0 \ Node1 \ Node2 Ex node: Test

But parentNode always returns null .

Can anyone help me?

    
asked by anonymous 04.07.2016 / 22:24

1 answer

0

I was able to solve my problems

First Czech the main nodes:

public void AddParent(string path, string node)
{
    foreach (TreeNode tnode in treeView1.Nodes)
    {
        if (tnode.FullPath == path)
        {
            tnode.Nodes.Add(node);
            break;
        }

        checkChildren(tnode, path, node);
    }

    treeView1.ExpandAll();
}

Then check the nodes that are children:

public void checkChildren(TreeNode original, string path, string node)
{
    foreach (TreeNode tnode in original.Nodes)
    {
        if (tnode.FullPath == path)
        {
            tnode.Nodes.Add(node);
            break;
        }

        checkChildren(tnode, path, node);
    }
}

Thank's!

    
04.07.2016 / 23:03