• 12
• 12
• 9
• 10
• 13

# [.net] Switching Tree Nodes

This topic is 4323 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

How do you reorder tree nodes without copying them, wiping the tree view then placing them back. I'm trying to make a simple section of code that shift a node upwards (so you can reorder nodes in a treeview manually), but I can't think of any way else to do it. Thanks

##### Share on other sites
Well each node in the tree has a collection of other nodes. So all you should have to do Add the node to its new parent, and remove it from the old.

Basically something like this:

'first you add the node to the new parent.NewParentNode.Nodes.Add(OldParent.Items(NodeIndex))'now remove it from the old parentOldParentNode.Nodes.Remove(OldParent.Items(NodeIndex))'Alternativly you can use the RemoveAt method:'OldParentNode.Nodes.RemoteAt(NodeIndex)

this assumes that NodeIndex is the index of the node you want.
you could find the selected node by using the SelectedNode member of the Treeview control. Then to determine the selected node's parent, you can use SelectedNode.Parent.

Now if you are talking about the order of the children in a single node, then you only have use the TreeNodeCollection Insert and RemoveAt/Remove methods. You remove the node from the collection, then Insert to put the node back in its new place. I'm not sure about removeing first, and inserting, or insert then remove... I'm not sure what would happen if you tried putting the same node in the collection twice.

If it helps, check out the documentation on the TreeView, TreeNode, and TreeNodeCollection

[Edited by - zangetsu on May 21, 2006 5:22:39 AM]