Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1077
  • Last Modified:

Re-order Nodes in TreeView

In VB6, using the TreeView control:

I need to let the user re-order nodes under a parent node, manually, by drag and drop (question is not about drag and drop details).
When I drop the source node, I can add a copy to the target position and delete the source node. However, this gets complicated when the source node has children.
Unfortunately, the Previous and Next properties of a node are read-only.

QUESTION is,
What wuld be an elegant method of moving nodes around, under the same parent ?
0
campinas
Asked:
campinas
  • 3
  • 2
  • 2
2 Solutions
 
AzraSoundCommented:
To move the nodes around all you should have to do is alter the Parent, e.g.,

Set movingNode.Parent = TreeView.Nodes(newParentNode.Key)
0
 
campinasAuthor Commented:
In my case the parent is the same.
0
 
AzraSoundCommented:
Sorry, didn't read it closely enough.  Never had to do this myself before so not sure about the most elegant approach.  I guess it would imagine building the node along with its children in memory and re-attaching it as such.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
TILLCommented:
You should use the Previous /Next properties of a Node object. This make the entire operation similar to moving an object in a double linked list.
As easy as 123. :)

Regards,
TILL
0
 
campinasAuthor Commented:
TILL, Previous and Next properties are read-only.
0
 
TILLCommented:
Well... you're right... But Parent is not. You can use this to create a new node and relocate all childs of initial node to the new node. This allows you to move the node without recreating the entire sub-structure:

Dim Node1 As Node
    Dim Node2 As Node
    Dim Node3 As Node
    Dim Node4 As Node
    Dim Node5 As Node
    Dim RootNode As Node
    Dim ChildNode As Node
   
    Set RootNode = TreeView1.Nodes.Add(, , , "Root")
    Set Node1 = TreeView1.Nodes.Add(RootNode, tvwChild, , "Item1")
    Set Node2 = TreeView1.Nodes.Add(RootNode, tvwChild, , "Item2")
    Set Node3 = TreeView1.Nodes.Add(RootNode, tvwChild, , "Item3")
    Set Node4 = TreeView1.Nodes.Add(RootNode, tvwChild, , "Item4")
   
   
    TreeView1.Nodes.Add Node1, tvwChild, , "Item11"
    TreeView1.Nodes.Add Node1, tvwChild, , "Item12"
    TreeView1.Nodes.Add Node1, tvwChild, , "Item13"
    TreeView1.Nodes.Add Node1, tvwChild, , "Item14"
   
    TreeView1.Nodes.Add Node2, tvwChild, , "Item21"
    TreeView1.Nodes.Add Node2, tvwChild, , "Item22"
    TreeView1.Nodes.Add Node2, tvwChild, , "Item23"
    TreeView1.Nodes.Add Node2, tvwChild, , "Item24"
   
    TreeView1.Nodes.Add Node3, tvwChild, , "Item31"
    TreeView1.Nodes.Add Node3, tvwChild, , "Item32"
    TreeView1.Nodes.Add Node3, tvwChild, , "Item33"
    TreeView1.Nodes.Add Node3, tvwChild, , "Item34"
   
    TreeView1.Nodes.Add Node4, tvwChild, , "Item41"
    TreeView1.Nodes.Add Node4, tvwChild, , "Item42"
    TreeView1.Nodes.Add Node4, tvwChild, , "Item43"
    TreeView1.Nodes.Add Node4, tvwChild, , "Item44"
   
    Set Node5 = TreeView1.Nodes.Add(Node1, tvwPrevious, , "Item5")
   
   
   
    While Not (Node4 Is Nothing)
        Set ChildNode = Node4.Child.FirstSibling
        Set ChildNode.Parent = Node5
        If Not Node4.Child Is Nothing Then
            Set ChildNode = Node4.Child.FirstSibling
        Else
            TreeView1.Nodes.Remove (Node4.Index)
           
            Set Node4 = Nothing
        End If
    Wend
0
 
campinasAuthor Commented:
Moving nodes by changing parent doesn't give control over position under the new parent... Rebuilding the node is however not apealing for large nodes... It would be nice to have this "move in branch" feature built into the TreeView control. Thank you both for your hints!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now