Reputation: 2407
I want to check if a treenode is exists and if exists then add child to that existing TreeNode.
Suppose I have a TreeNode treeroot and a child treechild1. I have created treenode like this:
TreeNode[] tchild = new TreeNode[] {"childe1"};
TreeNode troot = new TreeNode("treeroot",tchild);
Now I want to check if the treeroot is created or not. If created then i want to add another child to that node which i get after checking. How can I do this?
Upvotes: 2
Views: 7715
Reputation: 94635
You may try Nodes.Find("key",bool searchAllChildren)
method. To use this method you have to add key-value tree node.
For instance,
//Add First node
TreeView1.Nodes.Add("Root","Root");
and define a method that search and add a node,
public void SearchAndAdd(string searchKey, string newValue)
{
TreeNode[] list = treeView1.Nodes.Find(searchKey, true);
if (list.Length != 0)
{
list[0].Nodes.Add(newValue,newValue);
}
}
Call SearchAndAdd
method to add a node at given key,
SearchAndAdd("Root","First"); //added under Root
SearchAndAdd("Root","Second"); // do
SearchAndAdd("Second","2"); // added at Second
Upvotes: 7