none
Treeview Add Previous Child Node/Sibling RRS feed

  • Question

  • I am trying to add a Child Node to a TreeView. I'm converting my VB6 Treeview to the .NET Treeview control and I've figured out how to add the next Child Node to a Parent Node. But how do I add a Previous Node? Or should I call it adding a Previous Sibling? I posted an image below of the before and after of what I am trying to accomplish and what the Treeview should look like in the end.

    Monday, December 30, 2019 4:27 PM

Answers

  • Hi

    It is difficult yo be precise with a TreeView node structure. The first problem is that a TreeNode text can appear multiple times in different Nodes and so how to differentiate.

    In your example, it is possible that a node named TEST occurs in other branches also, so what do you do to differentiate that particular TEST node? Do you have a reference being used that identifies the current PARENT node?

    As you can see, it is near impossible to offer any solution when you don't show any code at all.

    It would probably be helpful if you show the code you use to add the next Child Node to a Parent Node -  then, knowing that, if you add a new node to the Parent, might be the answer toyour question.

        ' add a new node to nodes
        TreeView1.SelectedNode.Parent.Nodes.Add("NewNode", "NewNode")

        'insert a new node at index 0 of nodes
        TreeView1.SelectedNode.Parent.Nodes.Insert(0, "NewNode", "NewNode")


    Regards Les, Livingston, Scotland

    • Marked as answer by JMANCI Monday, December 30, 2019 7:18 PM
    Monday, December 30, 2019 4:58 PM

All replies

  • You are looking for the Insert method of the tree node collection.

    https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.treenodecollection.insert?view=netframework-4.8

    A simple example

            TreeView1.Nodes.Add("root") 'add root
            TreeView1.Nodes(0).Nodes.Add("ChildA") 'add child to root
            'tree looks like
            ' root
            '   ChildA
            TreeView1.Nodes(0).Nodes.Insert(0, "ChildI") 'insert this child before ChildA
            'tree looks like
            ' root
            '   ChildI
            '   ChildA
    


    Search Documentation

    SerialPort Info

    Multics - An OS ahead of its time.

     "Those who use Application.DoEvents have no idea what it does

        and those who know what it does never use it."    former MSDN User JohnWein

    Monday, December 30, 2019 4:58 PM
  • Hi

    It is difficult yo be precise with a TreeView node structure. The first problem is that a TreeNode text can appear multiple times in different Nodes and so how to differentiate.

    In your example, it is possible that a node named TEST occurs in other branches also, so what do you do to differentiate that particular TEST node? Do you have a reference being used that identifies the current PARENT node?

    As you can see, it is near impossible to offer any solution when you don't show any code at all.

    It would probably be helpful if you show the code you use to add the next Child Node to a Parent Node -  then, knowing that, if you add a new node to the Parent, might be the answer toyour question.

        ' add a new node to nodes
        TreeView1.SelectedNode.Parent.Nodes.Add("NewNode", "NewNode")

        'insert a new node at index 0 of nodes
        TreeView1.SelectedNode.Parent.Nodes.Insert(0, "NewNode", "NewNode")


    Regards Les, Livingston, Scotland

    • Marked as answer by JMANCI Monday, December 30, 2019 7:18 PM
    Monday, December 30, 2019 4:58 PM
  • Not impossible at all - your suggestion helped and it works perfectly. This is the line of code I am using, adding a key to the node and making it bold. Thanks for your help!

                If TreeView1.SelectedNode IsNot Nothing Then
    
                    TreeView1.SelectedNode.Parent.Nodes.Insert(0, sNewKey, "Previous").NodeFont = New Font(TreeView1.Font, FontStyle.Bold)
    
                End If

    Monday, December 30, 2019 7:18 PM