none
Tree View RRS feed

  • Question

  • Hey,

    I have tried to add a child node to a child note in a Tree View. But I haven't found a way to do it yet so I hoping that a smart person would be so kind to help me :)

    'Level 1
    TreeView1.Nodes.Add Key:="Group 1", Text:="National Group 1"
    TreeView1.Nodes.Add Key:="Group 2", Text:="National Group 2"
    TreeView1.Nodes.Add Key:="Group 3", Text:="National Group 3"

    'Level 2
    TreeView1.Nodes.Add "Group 1", tvwChild, Text:="SUB Group 1.1"
    TreeView1.Nodes.Add "Group 1", tvwChild, Text:="SUB Group 1.2"

    'Level 3
    TreeView1.Nodes.Add "SUB Group 1.1", tvwChild, Text:="SUB Group 1.1.1"
    TreeView1.Nodes.Add "SUB Group 1.2", tvwChild, Text:="SUB Group 1.1.2"

    Monday, July 2, 2018 3:32 PM

Answers

  • Hello Nikki Dee,

    We need specific a reference node while adding child node under a node. If we does not specific the reference node, the node will be added in the root nodes.

    I would suggest you test if below code work for you.

        'add node to root node
        TreeView1.Nodes.Add Key:="root", Text:="root"
    
        'add a child node under the root node
        TreeView1.Nodes.Add TreeView1.Nodes("root"), tvwChild, Key:="child level 1", Text:="childe node"
        
        'add a child node under the child node1 "child level 1"
        TreeView1.Nodes.Add TreeView1.Nodes("child level 1"), tvwChild, "child level 2", "childe node 2"

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, July 3, 2018 6:54 AM

All replies

  • You need to use the Nodes property if you want to go deeper in the tree. For example:

    treeView1.Nodes[1].Nodes.Add(new TreeNode("Hello"));
    

    The Windows Forms TreeView control stores the top-level nodes in its Nodes collection. Each TreeNode also has its own Nodes collection to store its child nodes. Both collection properties are of type TreeNodeCollection, which provides standard collection members that enable you to add, remove, and rearrange the nodes at a single level of the node hierarchy.


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Monday, July 2, 2018 4:21 PM
  • Hi Eugene,

    Thank you for your answer.

    I forgot to mention that I trying to make a tree View in Excel VBA. 

    TreeView1.Nodes(1)

    what is Nodes(1) referees to? the first parent/root node?

    Or does it works like

    Root

    Parantnode

    Childnode

    Monday, July 2, 2018 6:05 PM
  • Hello Nikki Dee,

    We need specific a reference node while adding child node under a node. If we does not specific the reference node, the node will be added in the root nodes.

    I would suggest you test if below code work for you.

        'add node to root node
        TreeView1.Nodes.Add Key:="root", Text:="root"
    
        'add a child node under the root node
        TreeView1.Nodes.Add TreeView1.Nodes("root"), tvwChild, Key:="child level 1", Text:="childe node"
        
        'add a child node under the child node1 "child level 1"
        TreeView1.Nodes.Add TreeView1.Nodes("child level 1"), tvwChild, "child level 2", "childe node 2"

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, July 3, 2018 6:54 AM
  • Hi Terry,

    Thank you very much for your help.

    I get it now I believe, as your to treeview1.Nodes.Add treeview1.Nodes(Index, as reference the first child)

    Thank you again :)

    • Marked as answer by Nikki Dee Tuesday, July 3, 2018 1:42 PM
    • Unmarked as answer by Nikki Dee Tuesday, July 3, 2018 1:42 PM
    Tuesday, July 3, 2018 1:42 PM
  • Hi Terry,

    One other question. I'm using a number as the root key, where the highest number contain 10 digits, but lower down some of them are only on 8 digits which give me an error as invalid key, but If I add 2 more digits then no problem.

    Does the key in root and child need to be on the exact same length?

    Best

    Nikki

    Tuesday, July 3, 2018 8:11 PM