Solved

How to insert a node into a treeview?

Posted on 2007-03-20
4
2,362 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Set form below another form 3 26
C# Error - Add Failed 12 53
Visual Studio editor? 1 18
Red error squiggly on vb.net 7 14
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now