|
-
Jun 8th, 2002, 08:36 AM
#1
Thread Starter
Member
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?!
-
Jun 8th, 2002, 09:53 AM
#2
Stuck in the 80s
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:
Option Explicit
Private Sub cmdDown_Click()
Dim nodX As Node, nodN As Node
Set nodX = TreeView1.SelectedItem
If Not [b]nodX.Next[/b] Is Nothing Then
With TreeView1
Set nodN = .Nodes.Add(nodX.Next, tvwNext, , nodX.Text)
nodN.Selected = True
If nodX.Children <> 0 Then
GetChildren TreeView1, nodX, nodN
End If
.Nodes.Remove nodX.Index
Set nodX = Nothing
End With
End If
End Sub
Private Sub cmdUP_Click()
Dim nodX As Node, nodN As Node
Set nodX = TreeView1.SelectedItem
If Not nodX.Previous Is Nothing Then
With TreeView1
Set nodN = .Nodes.Add(nodX.Previous, tvwPrevious, , nodX.Text)
nodN.Selected = True
If nodX.Children <> 0 Then
GetChildren TreeView1, nodX, nodN
End If
.Nodes.Remove nodX.Index
Set nodX = Nothing
End With
End If
End Sub
Private Sub Form_Load()
Dim nodX As Node, nodA As Node
Dim i As Integer, j As Integer, k As Integer
For i = 1 To 10
Set nodX = TreeView1.Nodes.Add(, , , "Grand Parent #" & i)
For j = 1 To 5
Set nodA = TreeView1.Nodes.Add(nodX.Index, tvwChild, , "Parent #" & j & " Child of #" & i)
If j = 3 Then
For k = 1 To 2
TreeView1.Nodes.Add nodA.Index, tvwChild, , "GrandChild of Parent #" & j
Next
End If
Next
Next
End Sub
Private Sub GetChildren(tvw As TreeView, nodN As Node, nodP As Node)
Dim nodC As Node, nodT As Node
Dim i As Integer
With tvw
For i = 1 To nodN.Children
If i = 1 Then
Set nodC = .Nodes.Add(nodP.Index, tvwChild, , nodN.Child.Text)
Set nodT = nodN.Child.Next
If nodN.Child.Children <> 0 Then
GetChildren tvw, nodN.Child, nodC
End If
Else
On Error Resume Next
Set nodC = .Nodes.Add(nodP.Index, tvwChild, , nodT.Text)
If nodT.Children <> 0 Then
GetChildren tvw, nodT, nodC
End If
Set nodT = nodT.Next
End If
Next
End With
End Sub
Edit: Note the bolded change.
Last edited by The Hobo; Jun 8th, 2002 at 10:02 AM.
-
Jun 8th, 2002, 09:56 AM
#3
Stuck in the 80s
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.
-
Jun 8th, 2002, 10:06 AM
#4
Stuck in the 80s
Optimized. It will now save the .Key property. I found an easy way to do it.
VB Code:
Option Explicit
Private Sub cmdDown_Click()
MoveNode TreeView1, TreeView1.SelectedItem, "DOWN"
End Sub
Private Sub cmdUP_Click()
MoveNode TreeView1, TreeView1.SelectedItem, "UP"
End Sub
Private Sub Form_Load()
Dim nodX As Node, nodA As Node
Dim i As Integer, j As Integer, k As Integer
'Populate our TreeView with dummy nodes
'3 levels deep so that we can play:
For i = 1 To 10
Set nodX = TreeView1.Nodes.Add(, , , "Grand Parent #" & i)
For j = 1 To 5
Set nodA = TreeView1.Nodes.Add(nodX.Index, tvwChild, , "Parent #" & j & " Child of #" & i)
If j = 3 Then
For k = 1 To 2
TreeView1.Nodes.Add nodA.Index, tvwChild, , "GrandChild of Parent #" & j
Next
End If
Next
Next
End Sub
'This is our recursive function to find the children
'Of a node, and that' nodes children, and so on.
Private Sub GetChildren(tvw As TreeView, nodN As Node, nodP As Node)
Dim nodC As Node, nodT As Node
Dim i As Integer
With tvw
'For each children in the tree
For i = 1 To nodN.Children
'If it's the first child:
If i = 1 Then
'Add the node:
Set nodC = .Nodes.Add(nodP.Index, tvwChild, , nodN.Child.Text)
'Set us up for the next child:
Set nodT = nodN.Child.Next
'Get the added nodes children:
If nodN.Child.Children <> 0 Then
GetChildren tvw, nodN.Child, nodC
End If
'It's not the first child:
Else
On Error Resume Next
'Add the node:
Set nodC = .Nodes.Add(nodP.Index, tvwChild, , nodT.Text)
'Get the added nodes children:
If nodT.Children <> 0 Then
GetChildren tvw, nodT, nodC
End If
'Set us up again:
Set nodT = nodT.Next
End If
Next
End With
End Sub
Private Sub MoveNode(tvw As TreeView, nodX As Node, Direction As String)
Dim nodN As Node
Dim strKey As String
'All we do here is copy the node and set it as the previous
'Nodes previous node. A little confusing, but it works.
'We then add all the children and delete the original
'Node
With tvw
Select Case Direction
Case "UP"
If Not nodX.Previous Is Nothing Then
Set nodN = .Nodes.Add(nodX.Previous, tvwPrevious, , nodX.Text)
Else
Exit Sub
End If
Case "DOWN"
If Not nodX.Next Is Nothing Then
Set nodN = .Nodes.Add(nodX.Next, tvwNext, , nodX.Text)
Else
Exit Sub
End If
End Select
nodN.Selected = True
If nodX.Children <> 0 Then
GetChildren tvw, nodX, nodN
End If
strKey = nodX.Key
.Nodes.Remove nodX.Index
Set nodX = Nothing
nodN.Key = strKey
End With
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.
-
Jun 8th, 2002, 10:30 AM
#5
Stuck in the 80s
Some feedback would be appreciated. Is there something else that needs to be added to it or is this good?
-
Jun 8th, 2002, 12:56 PM
#6
Thread Starter
Member
Thanks, haven't had time to try it yet, but when I do I'll let you know if it works!
-
Jun 8th, 2002, 01:13 PM
#7
Thread Starter
Member
Errr... I think it actually works!!!
THANK YOU!!!!!!!!!!!!!!
-
Jun 8th, 2002, 04:43 PM
#8
Stuck in the 80s
Let me know if there's any bugs or anything and I'll give them a shot
-
Jun 15th, 2002, 09:38 AM
#9
Thread Starter
Member
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
-
Jun 15th, 2002, 09:51 AM
#10
Stuck in the 80s
So is there "Still no solution?"
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|