vb.net treeview add sibling node

Joseph Mancini 81 Reputation points
2020-09-29T20:34:56.19+00:00

In vs2017, vb.net using windows forms, I would like to add a sibling node to a treeview. I upgraded from vs 2010 and the treeview control would use tvwPrevious or tvwNext as the relationship so I am looking for an equivalent with the newer Treeview control.

See attached treeview snippet- I highlighted the "Limited Partnership..." node and I am trying to add a new node directly below or above that selected node. The code I have now adds the new node to the very bottom of the tree:

TreeView1.Nodes.Insert(TreeView1.SelectedNode.Index, sNewKey, "PREVIOUS").NodeFont = New Font(TreeView1.Font, FontStyle.Bold)

I found various workarounds on the web, but nothing seems to work.

1 solution put the new node as a child to the selected node.

Another solution added the new node to the very end of the tree:
TreeView1.BeginUpdate()
TreeView1.Nodes.Insert(663, sNewKey, "PREVIOUS").NodeFont = New Font(TreeView1.Font, FontStyle.Bold) 'where 663 is the index of the selected node
TreeView1.EndUpdate()

Any ideas?

29222-capture.jpg

Developer technologies Visual Studio Other
0 comments No comments
{count} votes

Accepted answer
  1. Xingyu Zhao-MSFT 5,381 Reputation points
    2020-10-01T02:19:46+00:00

    Hi,
    Thanks for your feedback.
    The code you shared have some problems.

    How do I insert a new node before or after the node the user selected?

    You can refer to the following code to do this.

        Dim i As Integer = 1  
        Dim j As Integer = 1  
        Dim sNewKey As String = "key"  
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  
            If TreeView1.SelectedNode.Parent Is Nothing Then  
                If TreeView1.SelectedNode.Index = 0 Then  
                    TreeView1.Nodes.Insert(0, sNewKey, "N" + i.ToString()).NodeFont = New Font(TreeView1.Font, FontStyle.Bold)  
                Else  
                    TreeView1.Nodes.Insert(TreeView1.SelectedNode.Index, sNewKey, "N" + i.ToString()).NodeFont = New Font(TreeView1.Font, FontStyle.Bold)  
                End If  
            ElseIf TreeView1.SelectedNode.Index = 0 Then  
                TreeView1.SelectedNode.Parent.Nodes.Insert(0, sNewKey, "N" + i.ToString()).NodeFont = New Font(TreeView1.Font, FontStyle.Bold)  
            Else  
                TreeView1.SelectedNode.Parent.Nodes.Insert(TreeView1.SelectedNode.Index, sNewKey, "N" + i.ToString()).NodeFont = New Font(TreeView1.Font, FontStyle.Bold)  
            End If  
            i += 1  
        End Sub  
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click  
            If TreeView1.SelectedNode.Parent Is Nothing Then  
                TreeView1.Nodes.Insert(TreeView1.SelectedNode.Index + 1, sNewKey, "L" + j.ToString()).NodeFont = New Font(TreeView1.Font, FontStyle.Bold)  
            Else  
                TreeView1.SelectedNode.Parent.Nodes.Insert(TreeView1.SelectedNode.Index + 1, sNewKey, "L" + j.ToString()).NodeFont = New Font(TreeView1.Font, FontStyle.Bold)  
            End If  
            j += 1  
        End Sub  
    

    Result of my test.
    29468-gif.gif
    Hope it could be helpful.
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Xingyu Zhao-MSFT 5,381 Reputation points
    2020-09-30T06:07:13.793+00:00

    Hi JosephMancini-5735,
    Thank you for posting here.

    I would like to add a sibling node to a treeview

    I make a test on my side and add some treenodes into TreeView.
    29234-1.png
    Then I use the following code to add a sibling node to 'Great Grandchild'.

    TreeView1.Nodes(0).Nodes(1).Nodes(0).Nodes().Insert(1, (New TreeNode("Inserted node")))  
    

    Result of my test.
    29326-2.png
    Hope it could be helpful.

    0 comments No comments

  2. Joseph Mancini 81 Reputation points
    2020-09-30T16:10:14.91+00:00

    I see what you are doing, but I don't know how that would work for me. I need to detect what node the user selected on the tree, and add a new node before or after the node they selected. Your example drills down to that node, node by node, and inserts a new node. I can get the index of the node that the user selected. How do I insert a new node before or after the node the user selected? When I use the example on this link below, my new node gets inserted at the very bottom of my tree. I need a new node created at a specific location, based on the user selection.

    https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.treenodecollection.insert?view=netcore-3.1#System_Windows_Forms_TreeNodeCollection_Insert_System_Int32_System_String_System_String_

    0 comments No comments

  3. Joseph Mancini 81 Reputation points
    2020-09-30T19:53:38.523+00:00

    I did some more digging around today and found this link below with a solution in the last reply.
    https://www.vbforums.com/showthread.php?375905-Add-node-directly-after-selected-node

    So I applied this to my code, and came up with this to add the Next Sibling. It seems to work, as does adding a previous sibling, but perhaps there is a better way?

                If TreeView1.SelectedNode.Parent Is Nothing Then 'there is no parent of the selected node
                    'If we are on a parent/header node, then we use this line, which was the original line of code we were using
                    TreeView1.Nodes.Add(sNewKey, "Next").NodeFont = New Font(TreeView1.Font, FontStyle.Bold)
                Else 'we are adding to a parent/child/grandchild in the tree
                    TreeView1.SelectedNode.Parent.Nodes.Insert(TreeView1.SelectedNode.Index + 1, sNewKey, "Next").NodeFont = New Font(TreeView1.Font, FontStyle.Bold)
                End If
    
    0 comments No comments

  4. Joseph Mancini 81 Reputation points
    2020-10-01T15:33:18.263+00:00

    I tweaked your answer just a bit to display Next/Previous in the new node, but the answer does work for me as presented. Thank you.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.