Add Treeview Node by path

Asked

Viewed 78 times

0

Estu trying to add one TreeView Node by path, for example:

TreeView

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 No: Testing

But parentNode always returns null.

Can someone help me?

1 answer

0

I managed 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 I check the knots that are sons:

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!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.