locked
How to give a child node a new parent node? RRS feed

  • Question

  • I'm making a software application with a small treeview, but now I want to give a childnode a new parentnode . When this is the original situation:

    Parent1

           parent2

                  parent 3

                        child1

                        child2

                        child3

    ....I want to give child1 a new parent called: "parent4" and then i want this to be the situation afterwards:

    Parent1

           parent2

                  parent 3

                      Parent 4

                             child1

                             child2

                             child3

    But how do I accomplish this? By the way If i want to give parent 3 a new parent (parent 4), this should be the situation:

    Parent1

           parent2

                  parent 4

                      Parent 3

                             child1

                             child2

                             child3

    anybody has an idea?

    Thursday, February 21, 2013 8:23 PM

Answers

  •  I think i have found the solution. I Use this code now and it works the way i want it:

      newnode.Name = name
                            newnode.ImageIndex = imageindex
                            newnode.SelectedImageIndex = imageindex
                            Dim _parentnode As TreeNode = New TreeNode
                            _parentnode = selectedtreeviewnode.Parent
    
                            Dim nodes() As TreeNode = _parentnode.Nodes.Cast(Of TreeNode).ToArray
                            Dim i As Integer
                            For i = 0 To nodes.Count - 1
                                Tfamilytree.Nodes.Remove(nodes(i))
                            Next
                            '_parentnode.Nodes.Add(newnode)
                            _parentnode.Nodes.Add(newnode)
                            newnode.Nodes.AddRange(nodes)
    Sometimes it is a good habit to write out an algorithm in very tiny steps.

    • Edited by loi-se Friday, February 22, 2013 2:51 PM
    • Marked as answer by Mike Feng Thursday, February 28, 2013 1:53 PM
    Friday, February 22, 2013 2:50 PM

All replies

  • This is the same question that you asked before.

    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/8443b305-29aa-40ff-b289-dd6e94f3b6c1

    Deleting a node above the nodes you want to preserve involves moving the child sub-tree to the node above the one that you want to delete.

    Inserting a new node into the hierarchy involves moving the child sub-tree to the new node.

    The procedure is the same - the only thing that changes is the node you select.

    • Proposed as answer by Mike Feng Friday, February 22, 2013 8:56 AM
    • Marked as answer by Mike Feng Thursday, February 28, 2013 1:52 PM
    • Unmarked as answer by Mike Feng Thursday, February 28, 2013 1:53 PM
    Friday, February 22, 2013 12:06 AM
  • This is the same question that you asked before.

    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/8443b305-29aa-40ff-b289-dd6e94f3b6c1

    Deleting a node above the nodes you want to preserve involves moving the child sub-tree to the node above the one that you want to delete.

    Inserting a new node into the hierarchy involves moving the child sub-tree to the new node.

    The procedure is the same - the only thing that changes is the node you select.

    Yeah you are right but somehow the solution proposed in this topic doesn;t work out as I want.

    When I use this piece of code........:

     newnode.Name = name
        newnode.ImageIndex = imageindex
                            newnode.SelectedImageIndex = imageindex
    
                            Tfamilytree.Nodes.Add(newnode)
    
                            Dim pn1 As TreeNode = Tfamilytree.Nodes.Find(selectedtreeviewnode.Name, True).FirstOrDefault()
                            If pn1 IsNot Nothing Then
                                Tfamilytree.Nodes.Remove(pn1)
                                newnode.Nodes.Add(pn1)
                            End If

    ...........The node is added at the root of the treeview and not just above the node I selected. So if I want to give child1 a new parent called: "parent4" then this is the situation afterwards:

    Parent4

         Parent1

              parent2

                     parent 3

                            child1

                            child2

                            child3

    But this is not what I want,  I want this to be the situation afterwards:

    Parent1

           parent2

                  parent 3

                      Parent 4

                             child1

                             child2

                             child3

    So anybody has an idea what goes wrong?




    • Edited by loi-se Friday, February 22, 2013 12:06 PM
    Friday, February 22, 2013 12:03 PM
  • The reason the node is added to the root of the treeview is because that's exactly what you've told it to do here:

    Tfamilytree.Nodes.Add(newnode)

    Instead of calling Add on the Treeview's Nodes collection (which is the root) you shoudl be adding it to the Nodes collection of "parent3"


    Blog: http://codemidden.wordpress.com

    Friday, February 22, 2013 1:18 PM
  • The reason the node is added to the root of the treeview is because that's exactly what you've told it to do here:

    Tfamilytree.Nodes.Add(newnode)

    Instead of calling Add on the Treeview's Nodes collection (which is the root) you shoudl be adding it to the Nodes collection of "parent3"


    Blog: http://codemidden.wordpress.com

    Thanks for your suggestion. So if I understand you right the code should be as followed:

     newnode.Name = name
                            newnode.ImageIndex = imageindex
                            newnode.SelectedImageIndex = imageindex
                            Dim _parentnode As TreeNode = New TreeNode
                            _parentnode = selectedtreeviewnode.Parent
    
    
                            '_parentnode.Nodes.Add(newnode)
    
                            Dim pn1 As TreeNode = Tfamilytree.Nodes.Find(selectedtreeviewnode.Name, True).FirstOrDefault()
                            If pn1 IsNot Nothing Then
                                Tfamilytree.Nodes.Remove(pn1)
                                _parentnode.Nodes.Add(pn1)
                            End If

    So this is what I do now but the new node is still added to the root. Do you see what goes wrong?

    Friday, February 22, 2013 1:31 PM
  • You don't explain how you are determining what selectedtreeviewnode is.  Based on what I see with

    _parentnode = selectedtreeviewnode.Parent
    

    and

    _parentnode.Nodes.Add(pn1)

    it would suggest that selectedtreeviewnode is a direct child of root.  But I don't think so otherwise you wouldn't be so confused by all this. 

    Instead I'm going to go out on a limb and suggest that your Find call - which is using Name - may not be doing what you think.  Is it possible that your nodes may be sharing the same name among them?  This would cause the FirstOrDefault() call to find the first node on the treeview with the name you are searching on and likely cause unexpected results.  COuld this be the cause?


    Blog: http://codemidden.wordpress.com

    Friday, February 22, 2013 1:41 PM
  • What i mean with the selectedtreeviewnode can be seen in the image below:

    So in this picture ggggggg gggggg is the selected person ( shaded lightly grey) and this person should have a new mother or father. The selectednode get's  'assigned' like this:

    Private Sub Tfamilytree_NodeMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles Tfamilytree.NodeMouseClick
    
              
                selectedtreeviewnode = e.Node
    
           
        End Sub

    But the problem cannot be that some nodes have the same name because that is impossible because I check if the name already exists in the collection before the node get's added. So every node get's a name and a name that is unique.

    But the code I use now is as followed:

      newnode.Name = name
                            newnode.ImageIndex = imageindex
                            newnode.SelectedImageIndex = imageindex
                            Dim _parentnode As TreeNode = New TreeNode
                            _parentnode = selectedtreeviewnode.Parent
    
    
                            '_parentnode.Nodes.Add(newnode)
    
                            Dim pn1 As TreeNode = Tfamilytree.Nodes.Find(selectedtreeviewnode.Name, True).FirstOrDefault()
                            If pn1 IsNot Nothing Then
                                Tfamilytree.Nodes.Remove(pn1)
                                _parentnode.Nodes.Add(pn1)
                            End If

    Does anybody see what goes wrong?


    • Edited by loi-se Friday, February 22, 2013 2:06 PM
    Friday, February 22, 2013 2:02 PM
  •  I think i have found the solution. I Use this code now and it works the way i want it:

      newnode.Name = name
                            newnode.ImageIndex = imageindex
                            newnode.SelectedImageIndex = imageindex
                            Dim _parentnode As TreeNode = New TreeNode
                            _parentnode = selectedtreeviewnode.Parent
    
                            Dim nodes() As TreeNode = _parentnode.Nodes.Cast(Of TreeNode).ToArray
                            Dim i As Integer
                            For i = 0 To nodes.Count - 1
                                Tfamilytree.Nodes.Remove(nodes(i))
                            Next
                            '_parentnode.Nodes.Add(newnode)
                            _parentnode.Nodes.Add(newnode)
                            newnode.Nodes.AddRange(nodes)
    Sometimes it is a good habit to write out an algorithm in very tiny steps.

    • Edited by loi-se Friday, February 22, 2013 2:51 PM
    • Marked as answer by Mike Feng Thursday, February 28, 2013 1:53 PM
    Friday, February 22, 2013 2:50 PM