Link to home
Start Free TrialLog in
Avatar of blitzzy
blitzzy

asked on

How to insert a node into a treeview?

How do you INSERT -- not add -- a new item into a Treeview using VB.NET 2005?

For instance, if the user clicks on Parent 0, Child 2... the new item will be inserted into Child 2's position and Child 2 will move down.

Treeview Format:
Parent 0
    Child 0
    Child 1
    Child 2
Parent 1
    Child 0
    Child 1

I've tried this but nothing happens -- no error messages, either. It's driving me nuts! Please help!

   TreeView1.SelectedNode.Nodes.Insert(TreeView1.SelectedNode.Index, TextBox1.Text)
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Something like...

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Not (TreeView1.SelectedNode Is Nothing) Then
            Dim parentNode As TreeNode = TreeView1.SelectedNode.Parent
            If Not (parentNode Is Nothing) Then
                parentNode.Nodes.Insert(TreeView1.SelectedNode.Index, TextBox1.Text)
            Else
                TreeView1.Nodes.Insert(TreeView1.SelectedNode.Index, TextBox1.Text)
            End If
        End If
    End Sub
Avatar of blitzzy
blitzzy

ASKER

If I wanted to insert an existing treenode to another position, I'd have to Clone it first then remove it before inserting, right?
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of blitzzy

ASKER

Thanks for the help, Idle_Mind!