Results 1 to 10 of 10

Thread: Still no solution! Moving Tree Nodes Up/Down

  1. #1

    Thread Starter
    Member
    Join Date
    May 2002
    Location
    Great Britain
    Posts
    60

    Unhappy Still no solution! Moving Tree Nodes Up/Down

    I've posted about this numerous times and the replys I've had only work under certain conditions.

    I have a TreeView control and two buttons which Move the Nodes Up and Down in the tree, moving all of each node's children, and all of their children's children etc...

    Isn't there anyone who can answer this?!

  2. #2
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    This is the best I could come up with. It should be recursive so that it gets every child in the structure of the moved node:

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub cmdDown_Click()
    4. Dim nodX As Node, nodN As Node
    5.  
    6.   Set nodX = TreeView1.SelectedItem
    7.   If Not [b]nodX.Next[/b] Is Nothing Then
    8.     With TreeView1
    9.      
    10.       Set nodN = .Nodes.Add(nodX.Next, tvwNext, , nodX.Text)
    11.       nodN.Selected = True
    12.      
    13.       If nodX.Children <> 0 Then
    14.         GetChildren TreeView1, nodX, nodN
    15.       End If
    16.      
    17.       .Nodes.Remove nodX.Index
    18.       Set nodX = Nothing
    19.     End With
    20.   End If
    21. End Sub
    22.  
    23. Private Sub cmdUP_Click()
    24. Dim nodX As Node, nodN As Node
    25.  
    26.   Set nodX = TreeView1.SelectedItem
    27.   If Not nodX.Previous Is Nothing Then
    28.     With TreeView1
    29.      
    30.       Set nodN = .Nodes.Add(nodX.Previous, tvwPrevious, , nodX.Text)
    31.       nodN.Selected = True
    32.      
    33.       If nodX.Children <> 0 Then
    34.         GetChildren TreeView1, nodX, nodN
    35.       End If
    36.      
    37.       .Nodes.Remove nodX.Index
    38.       Set nodX = Nothing
    39.     End With
    40.   End If
    41. End Sub
    42.  
    43. Private Sub Form_Load()
    44. Dim nodX As Node, nodA As Node
    45. Dim i As Integer, j As Integer, k As Integer
    46.  
    47.   For i = 1 To 10
    48.     Set nodX = TreeView1.Nodes.Add(, , , "Grand Parent #" & i)
    49.     For j = 1 To 5
    50.       Set nodA = TreeView1.Nodes.Add(nodX.Index, tvwChild, , "Parent #" & j & " Child of #" & i)
    51.       If j = 3 Then
    52.         For k = 1 To 2
    53.           TreeView1.Nodes.Add nodA.Index, tvwChild, , "GrandChild of Parent #" & j
    54.         Next
    55.       End If
    56.     Next
    57.   Next
    58. End Sub
    59.  
    60. Private Sub GetChildren(tvw As TreeView, nodN As Node, nodP As Node)
    61. Dim nodC As Node, nodT As Node
    62. Dim i As Integer
    63.  
    64.   With tvw
    65.     For i = 1 To nodN.Children
    66.       If i = 1 Then
    67.         Set nodC = .Nodes.Add(nodP.Index, tvwChild, , nodN.Child.Text)
    68.         Set nodT = nodN.Child.Next
    69.         If nodN.Child.Children <> 0 Then
    70.           GetChildren tvw, nodN.Child, nodC
    71.         End If
    72.       Else
    73.         On Error Resume Next
    74.         Set nodC = .Nodes.Add(nodP.Index, tvwChild, , nodT.Text)
    75.         If nodT.Children <> 0 Then
    76.           GetChildren tvw, nodT, nodC
    77.         End If
    78.         Set nodT = nodT.Next
    79.       End If
    80.     Next
    81.   End With
    82. End Sub

    Edit: Note the bolded change.
    Last edited by The Hobo; Jun 8th, 2002 at 10:02 AM.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  3. #3
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    One thing I should mention: This isn't going to work if you want to preserve the Key properties. I would suggest changing your code if you use the Key properties. I ran into alot of problems when I attempted to copy the Key over. I could make it work to preserve the keys, but it would require alot of variables and would just be a big mess.

    I hope this helps.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  4. #4
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Optimized. It will now save the .Key property. I found an easy way to do it.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub cmdDown_Click()
    4.   MoveNode TreeView1, TreeView1.SelectedItem, "DOWN"
    5. End Sub
    6.  
    7. Private Sub cmdUP_Click()
    8.   MoveNode TreeView1, TreeView1.SelectedItem, "UP"
    9. End Sub
    10.  
    11. Private Sub Form_Load()
    12. Dim nodX As Node, nodA As Node
    13. Dim i As Integer, j As Integer, k As Integer
    14.  
    15.   'Populate our TreeView with dummy nodes
    16.   '3 levels deep so that we can play:
    17.   For i = 1 To 10
    18.     Set nodX = TreeView1.Nodes.Add(, , , "Grand Parent #" & i)
    19.     For j = 1 To 5
    20.       Set nodA = TreeView1.Nodes.Add(nodX.Index, tvwChild, , "Parent #" & j & " Child of #" & i)
    21.       If j = 3 Then
    22.         For k = 1 To 2
    23.           TreeView1.Nodes.Add nodA.Index, tvwChild, , "GrandChild of Parent #" & j
    24.         Next
    25.       End If
    26.     Next
    27.   Next
    28. End Sub
    29.  
    30. 'This is our recursive function to find the children
    31. 'Of a node, and that' nodes children, and so on.
    32. Private Sub GetChildren(tvw As TreeView, nodN As Node, nodP As Node)
    33. Dim nodC As Node, nodT As Node
    34. Dim i As Integer
    35.  
    36.   With tvw
    37.     'For each children in the tree
    38.     For i = 1 To nodN.Children
    39.       'If it's the first child:
    40.       If i = 1 Then
    41.         'Add the node:
    42.         Set nodC = .Nodes.Add(nodP.Index, tvwChild, , nodN.Child.Text)
    43.         'Set us up for the next child:
    44.         Set nodT = nodN.Child.Next
    45.         'Get the added nodes children:
    46.         If nodN.Child.Children <> 0 Then
    47.           GetChildren tvw, nodN.Child, nodC
    48.         End If
    49.       'It's not the first child:
    50.       Else
    51.         On Error Resume Next
    52.         'Add the node:
    53.         Set nodC = .Nodes.Add(nodP.Index, tvwChild, , nodT.Text)
    54.         'Get the added nodes children:
    55.         If nodT.Children <> 0 Then
    56.           GetChildren tvw, nodT, nodC
    57.         End If
    58.         'Set us up again:
    59.         Set nodT = nodT.Next
    60.       End If
    61.     Next
    62.   End With
    63. End Sub
    64.  
    65. Private Sub MoveNode(tvw As TreeView, nodX As Node, Direction As String)
    66. Dim nodN As Node
    67. Dim strKey As String
    68.  
    69.   'All we do here is copy the node and set it as the previous
    70.   'Nodes previous node. A little confusing, but it works.
    71.   'We then add all the children and delete the original
    72.   'Node
    73.  
    74.   With tvw
    75.     Select Case Direction
    76.       Case "UP"
    77.         If Not nodX.Previous Is Nothing Then
    78.           Set nodN = .Nodes.Add(nodX.Previous, tvwPrevious, , nodX.Text)
    79.         Else
    80.           Exit Sub
    81.         End If
    82.       Case "DOWN"
    83.         If Not nodX.Next Is Nothing Then
    84.           Set nodN = .Nodes.Add(nodX.Next, tvwNext, , nodX.Text)
    85.         Else
    86.           Exit Sub
    87.         End If
    88.     End Select
    89.      
    90.     nodN.Selected = True
    91.      
    92.     If nodX.Children <> 0 Then
    93.       GetChildren tvw, nodX, nodN
    94.     End If
    95.      
    96.     strKey = nodX.Key
    97.     .Nodes.Remove nodX.Index
    98.     Set nodX = Nothing
    99.     nodN.Key = strKey
    100.   End With
    101. End Sub

    Let me know if this is what you need.

    Edit: Commented it a little.
    Last edited by The Hobo; Jun 8th, 2002 at 10:11 AM.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  5. #5
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Some feedback would be appreciated. Is there something else that needs to be added to it or is this good?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  6. #6

    Thread Starter
    Member
    Join Date
    May 2002
    Location
    Great Britain
    Posts
    60

    Talking

    Thanks, haven't had time to try it yet, but when I do I'll let you know if it works!

  7. #7

    Thread Starter
    Member
    Join Date
    May 2002
    Location
    Great Britain
    Posts
    60
    Errr... I think it actually works!!!

    THANK YOU!!!!!!!!!!!!!!

  8. #8
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Let me know if there's any bugs or anything and I'll give them a shot
    My evil laugh has a squeak in it.

    kristopherwilson.com

  9. #9

    Thread Starter
    Member
    Join Date
    May 2002
    Location
    Great Britain
    Posts
    60
    Only a small problem found, that it doesn't move the Icons and Tags (Which I needed), but I fixed it myself, otherwise it seems to be fine

  10. #10
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    So is there "Still no solution?"
    My evil laugh has a squeak in it.

    kristopherwilson.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width