Sign in to follow this  

[.net] Switching Tree Nodes

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

If you intended to correct an error in the post then please contact us.

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.
NewParentNode.Nodes.Add(OldParent.Items(NodeIndex))
'now remove it from the old parent
OldParentNode.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]

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

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