Sign in to follow this  

[.net] Switching Tree Nodes

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 this post

Link to post
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.
'now remove it from the old parent
'Alternativly you can use the RemoveAt method:

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]

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this