Answered by:
Moving treenode to specific index

Question
-
Hello people
I still consider myself rather noobish here, so bare with me...
I want to move a root (level 0) treenode up and down the same level. I have started out with this relatively easy bit of code that will move the selected treenode, including its childnodes, to the last index of the level:
-------------------------
Dim nodetobemoved As TreeNode = TreeView1.SelectedNode.Clone
TreeView1.SelectedNode.Remove()
TreeView1.Nodes.Add(nodetobemoved)-------------------------
However, I cant seem to find a way to insert (.add) the nodetobemoved at a specific index of the level. Surely there must be a way?
Monday, October 27, 2014 7:59 AM
Answers
-
Hi Ricky,
You can use the Insert method for that:
TreeView1.Nodes.Insert(index,treeNode)
- Marked as answer by Ricky Blunda Monday, October 27, 2014 9:23 AM
Monday, October 27, 2014 9:13 AM -
Hi Ricky,
With the SelectedMode you can easily set the focus to a treenode:
TreeView1.SelectedNode = nodetobemoved
Hope this helps- Marked as answer by Ricky Blunda Monday, October 27, 2014 9:45 AM
Monday, October 27, 2014 9:31 AM -
Continuing on this thread as it is highly related: how can you insert a node at a specific level? The .insert method only asks for index.
It seems that .insert always adds nodes on level 0...?
Ricky,
This is a whole different way to do all of the TreeView stuff, but you might want to consider it.
I have a class that I made to deal with these sorts of things. The following shows the methods (including what you're talking about that you want):
The code for the class is shown on a page of my website here.
Still lost in code, just at a little higher level.
:-)- Marked as answer by Ricky Blunda Wednesday, October 29, 2014 7:14 AM
Tuesday, October 28, 2014 8:12 PM -
Hi Ricky,
You can also add child nodes to an existing node. Following code add a childnode to the selected node:
treeView1.SelectedNode.Nodes.Add("ChildNode");
Hope this helps
- Marked as answer by Ricky Blunda Wednesday, October 29, 2014 7:14 AM
Tuesday, October 28, 2014 8:31 PM
All replies
-
Hi Ricky,
You can use the Insert method for that:
TreeView1.Nodes.Insert(index,treeNode)
- Marked as answer by Ricky Blunda Monday, October 27, 2014 9:23 AM
Monday, October 27, 2014 9:13 AM -
Oh my God, I cant believe it's that simple :P Many thanks, I can now simply make one "Move up"-button and one "Move down"-button with the following code:
--------------------------------
Dim location As Integer
location = TreeView1.SelectedNode.Index
Dim nodetobemoved As TreeNode = TreeView1.SelectedNode.Clone
TreeView1.SelectedNode.Remove()
TreeView1.Nodes.Insert(location - 1, nodetobemoved)--------------------------------
Simply replace the "- 1" with "+ 1" for down and up respectively. I can easily enough make an IF-statement so that you can only move nodes on root level the way I want.
Would be awesome if I could also keep focus on the node after it being moved, but I'm sure that's a minor issue to solve :)
Again, many thanks!
Monday, October 27, 2014 9:26 AM -
Hi Ricky,
With the SelectedMode you can easily set the focus to a treenode:
TreeView1.SelectedNode = nodetobemoved
Hope this helps- Marked as answer by Ricky Blunda Monday, October 27, 2014 9:45 AM
Monday, October 27, 2014 9:31 AM -
Continuing on this thread as it is highly related: how can you insert a node at a specific level? The .insert method only asks for index.
It seems that .insert always adds nodes on level 0...?
Tuesday, October 28, 2014 7:12 PM -
Continuing on this thread as it is highly related: how can you insert a node at a specific level? The .insert method only asks for index.
It seems that .insert always adds nodes on level 0...?
Ricky,
This is a whole different way to do all of the TreeView stuff, but you might want to consider it.
I have a class that I made to deal with these sorts of things. The following shows the methods (including what you're talking about that you want):
The code for the class is shown on a page of my website here.
Still lost in code, just at a little higher level.
:-)- Marked as answer by Ricky Blunda Wednesday, October 29, 2014 7:14 AM
Tuesday, October 28, 2014 8:12 PM -
Hi Ricky,
You can also add child nodes to an existing node. Following code add a childnode to the selected node:
treeView1.SelectedNode.Nodes.Add("ChildNode");
Hope this helps
- Marked as answer by Ricky Blunda Wednesday, October 29, 2014 7:14 AM
Tuesday, October 28, 2014 8:31 PM