Treenode has a collection, TreeNodeCollection (would you believe! . This
collection class has an item property that takes an index. So, yes it should
be pretty simple as long as you know the indices or relative indices!
... for all those who are almost desperate, here is what I use:
'before calling this sub in my code I've already checked if n1 and n2 are the children of 'the same parent node
Private Sub swapNodes(ByVal n1 As MyTreeNode, ByVal n2 As MyTreeNode, Optional ByVal nSelected As MyTreeNode = Nothing)
Dim nParent As MyTreeNode = n1.Parent
If nParent IsNothingThenExitSub 'in my code I prevent top level nodes swapping
Me.MTreeView1.BeginUpdate()
nParent.Nodes.RemoveAt(n2.Index)
nParent.Nodes.RemoveAt(n1.Index)
nParent.Nodes.Insert(n2.Index - 1, n1)
nParent.Nodes.Insert(n1.Index, n2)
Me.MTreeView1.Focus()
Me.MTreeView1.EndUpdate()
If nSelected Is Nothing Then nSelected = n2
MTreeView1.SelectedNode = nSelected
EndSub
Last edited:
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.