treeview - Move node, insert node 
Author Message
 treeview - Move node, insert node

I have a treeview that is already populated.  I would like to move the
selected item to a node that has a subnode and work my way down the subnode.
I guess what I'm saying is that i would like to move an item "down" a level.

Any thoughts?
Chris Thunell



Sun, 27 Feb 2005 22:53:45 GMT  
 treeview - Move node, insert node
Hi Chris,

Here is some sample code that  will do the trick. I have assumed that there
is only one child for the selected node. This child can have any number of
children but if there are more children of the selected node, they will be
removed.
You can however, follow the logic and find the siblings of the childNode
and then add, them back if you want.

        Dim curNode As TreeNode
        curNode = TreeView1.SelectedNode
        'Exit if selected node is RootNode
        If curNode Is TreeView1.Nodes(0) Then Exit Sub

        Dim pNode As TreeNode = curNode.Parent
        Dim cNode As TreeNode = curNode.FirstNode
        Dim ClonedNode As TreeNode
        'If there are any siblings to the childNode, they will be removed.
        If Not cNode Is Nothing Then
            'Clone child Node
            ClonedNode = cNode.Clone
            'Add selected Node as text to the cloned ChildNode
            ClonedNode.Nodes.Add(curNode.Text)
            'Add ClonedNode to the Parent Node
            pNode.Nodes.Add(ClonedNode)
            curNode.Remove()   'Remove selected Node
        Else
            'No childNode exists for the selected Node
            Exit Sub
        End If

Hope this helps!
Bharat Patel
Microsoft, Visual Basic .NET

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.              



Mon, 28 Feb 2005 03:50:46 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. TreeView-Move Node Between Other Nodes?

2. treeview node count not incrementing when adding new nodes

3. This is How to Move TreeView Nodes Around

4. This is how to move TreeView Nodes Around!!!

5. Moving nodes around in a treeview control

6. Help with Treeview control - moving nodes

7. Treeview - how to position node when moving?

8. Moving a node in a TreeView.

9. Moving nodes in Treeview with children

10. Moving nodes in Treeview with children

11. Move Treeview Child Node To Different Parent

12. Move a node in TreeView Control

 

 
Powered by phpBB® Forum Software