Solved

How to insert a node into a treeview?

Posted on 2007-03-20
4
2,364 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

895 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

17 Experts available now in Live!

Get 1:1 Help Now