How to move Root nodes up and down in a treeview Access/VBA
Ok. I've been working a lot on a treeview and I decided that it would be convenient to allow the users to move the nodes of the treeview up and down however they see fit. My structure is just a simple two level treeview, however every root node must have a child. For example:
Root
child
child
child
Root
child
child
Root
child
child
child
child
I have code written where you can only check off one box from the Root nodes at a time. What I want to do is click a button, and have the checked root node move up (or down) one position (of course taking its children with it).
The only way I can imagine this working would be to completely rebuild the node one level higher 开发者_开发百科than it was previously. This seems like it would be far too time consuming when the nodes begin to have more children etc.
When I searched I found a ton of C# results, but since I'm using VBA it didn't help me at all. If anybody has any suggestions outside of rebuilding the entire node I would love to hear it. Thanks
The way I solved this was by saving the keys and text of my nodes in temporary variables, clearing the keys, and then switching them.
At this point, I then loop through all of the children and add them into an array of Nodes. I set the nodes.Parent property of each of them to the opposing node, and then it was all pretty much done.
This is possible because the data in the table depends on how the user structures the treeview, so once they have the treeview displaying how they want it, I can save the information I need and rebuild it when they open that particular record back up. I hope this is clear but I have some code chunks below to help clear this up.
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
^The code above is made to move n up into n.Previous spot The following code, is to switch the children of the nodes (if any)
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
I hope this example will help anybody else who has a similar two level tree structure and is looking to do something like this. I am doing it on a button click, however it could be reworked for a drag and drop method.
精彩评论