P Phil G. Sep 26, 2005 #2 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
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 Sep 27, 2005 #3 Hi Phil, So, yes it should be pretty simple Click to expand... 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?
Hi Phil, So, yes it should be pretty simple Click to expand... 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?
A aa25 Joined Jul 16, 2010 Messages 1 Reaction score 0 Jul 16, 2010 #5 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: Jul 16, 2010
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