locked
Can you delete a parent node without deleting the child nodes? RRS feed

  • Question

  • I have a treeview in my software application and now I want to delete a parent node without deleting it's child nodes. So if this is the original situation.......:

    Parent1

           Parent2

                  Child

                  Child

                  Child

    ......I want to delete parent2 and get this situation:

    Parent1

                  Child

                  Child

                  Child

    But how do I accomplish this? If I just do this...:

     
    Tfamilytree.Nodes.Remove(selectedtreeviewnode)

    .....then the child nodes will also disappear. Can anybody give a code example of how to accomplish what i want?

    Thanks in advance!

    Saturday, February 16, 2013 7:00 PM

Answers

  • Here how to do

    Public Class Form1
    
        Dim Tree As New TreeView
        Dim MainNode As New TreeNode("MainNode")
        Dim Parent1Node As New TreeNode("Parent1")
        Dim Parent2Node As New TreeNode("Parent2")
        Dim Child1Node As New TreeNode("Child1")
        Dim Child2Node As New TreeNode("Child2")
        Dim Child3Node As New TreeNode("Child3")
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Tree.BeginUpdate()
            Tree.Nodes.Add(MainNode)
            MainNode.Nodes.Add(Parent1Node)
            Parent1Node.Nodes.Add(Parent2Node)
            Parent2Node.Nodes.AddRange({Child1Node, Child2Node, Child3Node}.ToArray)
            Tree.EndUpdate()
            Tree.ExpandAll()
            Tree.Parent = Me
            Tree.Width = 200
            Tree.Dock = DockStyle.Left
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            RemoveNodePreserveChilds(Parent2Node)
        End Sub
    
        Private Sub RemoveNodePreserveChilds(ByVal NodeToRemove As TreeNode)
            Dim NodeParent = NodeToRemove.Parent
            If NodeParent Is Nothing Then Return
            Dim NodesToPreserve As New List(Of TreeNode)
            For Each Node As TreeNode In NodeToRemove.Nodes
                NodesToPreserve.Add(Node)
            Next
            NodeParent.Nodes.Remove(NodeToRemove)
            For Each node As TreeNode In NodesToPreserve
                NodeToRemove.Nodes.Remove(node)
                NodeParent.Nodes.Add(node)
            Next
        End Sub
    
    End Class

    • Edited by Crazypennie Saturday, February 16, 2013 11:04 PM
    • Proposed as answer by Frank L. Smith Saturday, February 16, 2013 11:07 PM
    • Marked as answer by Youen Zen Thursday, February 21, 2013 8:39 AM
    Saturday, February 16, 2013 10:53 PM

All replies

  • Maybe these links will be of assistance

    http://forums.asp.net/t/1347603.aspx

    http://www.vbforums.com/showthread.php?515837-Solved-2008-Treeview-How-to-remove-Parent-but-keep-Child-s


    You've taught me everything I know but not everything you know.

    Saturday, February 16, 2013 8:07 PM
  • Here how to do

    Public Class Form1
    
        Dim Tree As New TreeView
        Dim MainNode As New TreeNode("MainNode")
        Dim Parent1Node As New TreeNode("Parent1")
        Dim Parent2Node As New TreeNode("Parent2")
        Dim Child1Node As New TreeNode("Child1")
        Dim Child2Node As New TreeNode("Child2")
        Dim Child3Node As New TreeNode("Child3")
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Tree.BeginUpdate()
            Tree.Nodes.Add(MainNode)
            MainNode.Nodes.Add(Parent1Node)
            Parent1Node.Nodes.Add(Parent2Node)
            Parent2Node.Nodes.AddRange({Child1Node, Child2Node, Child3Node}.ToArray)
            Tree.EndUpdate()
            Tree.ExpandAll()
            Tree.Parent = Me
            Tree.Width = 200
            Tree.Dock = DockStyle.Left
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            RemoveNodePreserveChilds(Parent2Node)
        End Sub
    
        Private Sub RemoveNodePreserveChilds(ByVal NodeToRemove As TreeNode)
            Dim NodeParent = NodeToRemove.Parent
            If NodeParent Is Nothing Then Return
            Dim NodesToPreserve As New List(Of TreeNode)
            For Each Node As TreeNode In NodeToRemove.Nodes
                NodesToPreserve.Add(Node)
            Next
            NodeParent.Nodes.Remove(NodeToRemove)
            For Each node As TreeNode In NodesToPreserve
                NodeToRemove.Nodes.Remove(node)
                NodeParent.Nodes.Add(node)
            Next
        End Sub
    
    End Class

    • Edited by Crazypennie Saturday, February 16, 2013 11:04 PM
    • Proposed as answer by Frank L. Smith Saturday, February 16, 2013 11:07 PM
    • Marked as answer by Youen Zen Thursday, February 21, 2013 8:39 AM
    Saturday, February 16, 2013 10:53 PM