We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Moving Treenodes

Does anybody know how to move a treenode under VB.Net. It used to be easy under VB6, you just changed the parent property. I want to allow users to move nodes within a parent node i.e Parent --Child1 --Child2 --Child3 will become :- Parent --Child1 --Child3 --Child2 Any ideas gratefully appreciated :-(

3 Replies

AD Administrator Syncfusion Team October 2, 2002 06:27 PM UTC

I believe can you just adjust the position of the child nodes in the Parent's Nodes collection. Using say Remove and Insert, for example. -Praveen


FR FruitBatInShades October 3, 2002 07:38 AM UTC

I've tried allsorts but the results are not reliable, you either get clones or the nodes end up appearing at the end! Do you have a code example?


AD Administrator Syncfusion Team October 3, 2002 11:12 AM UTC

The Nodes collection does not have an InsertAt method, so you may just have to rebuild the collection in the order that you want things. To do so, you can save all the nodes, then clear the Nodes collection, and add the nodes back in the order that you want them. Here is some code.
private void button1_Click(object sender, System.EventArgs e)
{
	//move treenode 3 to treenode 1
	int targetIndex = 1;
	int sourceIndex = 3;

	TreeNode tn = treeView1.Nodes[sourceIndex];
	TreeNode[] tnc = new TreeNode[treeView1.Nodes.Count];
	int k = 0;
	foreach(TreeNode t in treeView1.Nodes)
		tnc[k++] = t;
			
	treeView1.Nodes.Clear();

	for(int i = 0; i < k; i++)
	{
		if(i == targetIndex)
		{
			treeView1.Nodes.Add(tn);
		}
		if( !tn.Equals(tnc[i]))
		{
			treeView1.Nodes.Add(tnc[i]);
		}
	}
}

Loader.
Live Chat Icon For mobile
Up arrow icon