Hello,

I have a treeview control and want to add a node to it at a desired location or as the child of a Certain Node X.The problem is I will know nodename[or the Text displayed ] of X only at runtime.

How do I Iterate the tree to find the given Node and add a child to it ??

Do I have to write a recursive function that iterates the nodes of a tree,find the given node and add a child to it OR is there an easier way to do this..

Based on what information you want to add child nodes?
Can you explain more about why you want to do this, this will help us to provide better solution.

Hello,

I have a treeview control and want to add a node to it at a desired location or as the child of a Certain Node X.The problem is I will know nodename[or the Text displayed ] of X only at runtime.

How do I Iterate the tree to find the given Node and add a child to it ??

Do I have to write a recursive function that iterates the nodes of a tree,find the given node and add a child to it OR is there an easier way to do this..

Use the TreeView's TreeNodeCollection Find method:
public TreeNode[] Find(string key, bool searchAllChildren);

Then, peruse the returned array for your specific node and Add your new node.

If you are not actually using the TreeNode.Name (was unclear), you will need to use recursion on the TreeNode.Text. Let me know if you need help with that.

Hai,
Thanks for the Quick response..
If I do TreeView's TreeNodeCollection Find method it will return me a treenodecollection and any changes I do will not be reflected in the actula treeview.Please correct me if I am wrong..

Hai..
IF I am using Treenode.Name then how can I proceed to search for a given Node and append a child to it..??

Hai,
@DangerDev
I get a input from the user and based on this input there will be corresponding node in my tree view(Can be at any level inside the treeview),and I will know only its name .Have to find this node and append my child node to it..
Where I add my child Node depends on the user and has to be done at runtime..
Hope this Helps..

Hai,
Thanks for the Quick response..
If I do TreeView's TreeNodeCollection Find method it will return me a treenodecollection and any changes I do will not be reflected in the actula treeview.Please correct me if I am wrong..

It will return a reference to the node collection, so your changes will apply.

Hai,
@DangerDev
I get a input from the user and based on this input there will be corresponding node in my tree view(Can be at any level inside the treeview),and I will know only its name .Have to find this node and append my child node to it..
Where I add my child Node depends on the user and has to be done at runtime..
Hope this Helps..

If you are searching on the TreeNode text, you can use the following recursion:

    public static TreeNode FindTreeNodeText(TreeNodeCollection nodes, string findText)
    {
        TreeNode foundNode = null;
        for (int i = 0; i < nodes.Count && foundNode == null; i++)
        {
            if (nodes[i].Text == findText)
            {
                foundNode = nodes[i];
                break;
            }
            if (nodes[i].Nodes.Count > 0)
                foundNode = FindTreeNodeText(nodes[i].Nodes, findText);
        }
        return foundNode;
    }

The node collection is the TreeView's Nodes (tv.Nodes). If found, you simply foundNode.Add(new TreeNode());

Reposting method: I don't know why the tags didn't display the code properly. If this fixes your problem, please flag as Solved--thanx.

public static TreeNode FindTreeNodeText(TreeNodeCollection nodes, string findText)
        {
            TreeNode foundNode = null;
            for (int i = 0; i < nodes.Count && foundNode == null; i++)
            {
                if (nodes[i].Text == findText)
                {
                    foundNode = nodes[i];
                    break;
                }
                if (nodes[i].Nodes.Count > 0)
                    foundNode = FindTreeNodeText(nodes[i].Nodes, findText);
            }
            return foundNode;
        }

Thanx DoubleD it solved my problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.