Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 1.08 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Adding nodes to a specific parent node in a treeView (c#)
  2. using (var reader = File.OpenText("Configuration.ini"))
  3.             {
  4.                 List<string> hostnames = ParseExternalHosts(reader).ToList();
  5.                 foreach (string s in hostnames)
  6.                 {
  7.                     TreeNode newNode = new TreeNode(s);
  8.                     hostView.SelectedNode.Nodes.Add(newNode);
  9.                 }
  10.        
  11. const string nodeKey = "hostNode";
  12.  
  13. TreeNode tn1 = new TreeNode("My Node");
  14. tn1.Name = nodeKey; // This is the name (=key) for the node.
  15.  
  16. TreeNode tn2 = new TreeNode("My Node2");
  17. tn2.Name = "otherKey"; // This is the key for node 2.
  18.  
  19. treeView1.Nodes.Add(tn1); // Add node1.
  20. treeView1.Nodes.Add(tn2); // Add node2.
  21.        
  22. // Find node by name (=key). Use the key specified above for tn1.
  23. // If key is not unique you will get more than one node here.
  24. TreeNode[] found = treeView1.Nodes.Find(nodeKey, true);
  25.  
  26. // Do something with the found node - e.g. add just another node to the found node.
  27. TreeNode newChild = new TreeNode("A Child");
  28. newChild.Name = "newChild";
  29.  
  30. found[0].Nodes.Add(newChild);