Re: How to swap two treenodes in a treeview
... 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