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.
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.