我解决这个问题的方法是将节点的键和文本保存在临时变量中,清除键,然后切换它们。
此时,我将遍历所有子节点并将它们添加到节点数组中。我将它们每个的 nodes.Parent 属性设置为对立的节点,然后一切都完成了。
这是可能的,因为表中的数据取决于用户如何构建树形视图,所以一旦树形视图显示了他们想要的方式,我可以保存我需要的信息并在他们打开该特定记录时重建它向上。我希望这很清楚,但我在下面有一些代码块可以帮助解决这个问题。
Private Sub MoveUP()
Dim n As Node
Dim key1 As String
'etc.....
key1 = n.Key
text1 = n.Text
key2 = n.Previous.Key
text2 = n.Previous.Text
'We have to clear out the keys now.
n.Key = ""
n.Previous.Key = ""
'Now replace the keys
n.Key = key2
n.Text = text2
n.Previous.Key = key1
n.Previous.Text = text1
Call SwitchParents(n, n.Previous)
End Sub
^上面的代码是将n向上移动到n.Previous点
下面的代码,是切换节点的子节点(如果有的话)
Private Sub SwitchParents(n1 As Node, n2 As Node)
Dim nds1() As Node 'Declare with no size to ReDim later
Dim nds2() As Node
Dim c As Node 'this is the child node we will use to push into the array
Dim i As Integer
i = n1.Children
ReDim nds1(0 To i)
Set c = n1.Child
'Notice in both loops that i remains the number of children, the arrays fill backwards
'because when you reassign the nodes.Parent property, the node jumps to the beginning,
'so we pack them backwards and they come out displaying the same way they did before.
Do While Not (c Is Nothing)
i = i - 1
Set nds1(i) = c
Set c = c.Next
Loop
i = n2.Children
ReDim nds2(0 To i)
Set c = n2.Child
Do While Not (c Is Nothing)
i = i - 1
Set nds2(i) = c
Set c = c.Next
Loop
If Not IsEmpty(nds1()) Then
For i = 0 To UBound(nds1()) - 1
Set nds1(i).Parent = n2
Next i
End If
If Not IsEmpty(nds2()) Then
For i = 0 To UBound(nds2()) - 1
Set nds2(i).Parent = n1
Next i
End If
End Sub
我希望这个例子能帮助其他任何有类似的两级树结构并希望做这样的事情的人。我是在单击按钮时执行此操作的,但是可以将其重新设计为拖放方法。