1
votes

I have this code:

TreeNode newNodeText = null;
TreeNode newNodeParsed = null;
TreeNode rootNode = treeView1.Nodes[0];

if (!txtDir.Text.Contains("/"))
{
    newNodeText = new TreeNode(txtDir.Text);
    rootNode.Nodes.Add(newNodeText);
}
else
{
    List<string> test1 = txtDir.Text.Split('/').ToList();
    for (int i = 0; i < test1.Count; i++)
    {
        newNodeParsed = new TreeNode(test1[i]);
        rootNode.Nodes.Add(newNodeParsed);
    }                       
}

If in the textBox(txtDir) the string i type dosen't contain any '/'then the new node will be add under the root in the treeView1.

But if i type in the textBox(txtDir) for example test1/test2/test3/test4 Then i need that test1 will be under root under the rootNode but test2 will be inside test1 and test3 to be inside test2 and test4 inside test3

Inside i mean under like a sub directory.

I need do it in the else part for now it will just add it to the root.

1

1 Answers

2
votes

You can't keep adding the nodes to the same parent, so try updating the reference to the new parent as you loop:

TreeNode nextNode = rootNode;
for (int i = 0; i < test1.Count; i++) {
  newNodeParsed = new TreeNode(test1[i]);
  nextNode.Nodes.Add(newNodeParsed);
  nextNode = newNodeParsed;
}