Solved

How to insert a node into a treeview?

Posted on 2007-03-20
4
2,374 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
[X]
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
  • 2
  • 2
4 Comments
 
LVL 86

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 86

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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

717 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