how to swap two nodes of a treeview

P

Phil G.

Sam,

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!

HTH, Phil
 
S

Sam

Hi Phil,
So, yes it should be pretty simple
I wish it was:)

I can swap the text of two nodes no problem, which means my indexes are
correct at least. However when I swap the nodes, there is no effect at
all :

Dim n1 As TreeNode = FindControlNode(CType(obj1, Field))
Dim n2 As TreeNode = FindControlNode(CType(obj2, Field))

Dim tmp As TreeNode = n1
n1 = n2
n2 = tmp

Can you help?
 
Joined
Jul 16, 2010
Messages
1
Reaction score
0
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







 
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.

Ask a Question

Top