Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to insert a node into a treeview?

Posted on 2007-03-20
4
2,368 Views
Last Modified: 2013-11-26
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)
0
Comment
Question by:blitzzy
  • 2
  • 2
4 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18759600
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
0
 
LVL 1

Author Comment

by:blitzzy
ID: 18760092
If I wanted to insert an existing treenode to another position, I'd have to Clone it first then remove it before inserting, right?
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 125 total points
ID: 18760428
(1) Assign it to a local TreeNode variable.
(2) Remove it from its current position.
(3) Insert it at its new position.

        If Not (TreeView1.SelectedNode Is Nothing) Then
            Dim tn As TreeNode = TreeView1.SelectedNode
            TreeView1.Nodes.Remove(tn)
           
            ' Insert "tn" somewhere else...
            TreeView1.Nodes.Insert(xxx, tn)
        End If
0
 
LVL 1

Author Comment

by:blitzzy
ID: 18763341
Thanks for the help, Idle_Mind!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question