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.
^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)
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.