Hi Mathi2code,
I'd use a recursive initialization function. something like this: (you can run the example)
using System;
using System.Windows.Forms;
namespace RecursiveTreeViewNodes
{
static class Program
{
[STAThread]
static void Main()
{
Form form = new Form();
TreeView treeview = new TreeView() { Dock = DockStyle.Fill };
form.Controls.Add(treeview);
TreeNode nodeRoot = new TreeNode("root");
treeview.Nodes.Add(nodeRoot);
AddTreeNodesRecursive(nodeRoot, 4);
AddTreeNodesRecursive(nodeRoot, 9);
AddTreeNodesRecursive(nodeRoot, 3);
Application.Run(form);
}
static void AddTreeNodesRecursive(TreeNode nodeParent, int iDepth)
{
if (iDepth > 0 && nodeParent != null)
{
TreeNode nodeChild = new TreeNode(nodeParent.Text + ".1");
nodeParent.Nodes.Add(nodeChild);
AddTreeNodesRecursive(nodeChild, --iDepth);
}
}
}
}
This should give you just the General idea. If if you expect very deep nesting depths you should avoid recursion, it could be reforumlated to a loop if you are fearing StackOverflows.
Good luck with your project!
kind regards Johannes