2

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?

Jason Meckley
  • 7,589
  • 1
  • 24
  • 45
Md Kamruzzaman Sarker
  • 2,387
  • 3
  • 22
  • 38

1 Answers1

7

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
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186