// Creating a new TreeNode and adding it to a TreeView control TreeNode node = new TreeNode("ParentNode"); treeView1.Nodes.Add(node); // Creating a child TreeNode and adding it to the parent TreeNode TreeNode childNode = new TreeNode("ChildNode"); node.Nodes.Add(childNode);
// Traversing through all TreeNodes in a TreeView control foreach (TreeNode node in treeView1.Nodes) { Console.WriteLine(node.Text); foreach (TreeNode childNode in node.Nodes) { Console.WriteLine(childNode.Text); } }In this example, we are traversing through all the TreeNodes in a TreeView control. We first iterate through the top-level nodes and print out their text. Then, we iterate through each child node and print out their text as well. The package library used in these examples is the System.Windows.Forms namespace, which is part of the .NET Framework library.