?
Solved

Re-order Nodes in TreeView

Posted on 2004-08-02
7
Medium Priority
?
1,060 Views
Last Modified: 2012-06-21
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
Comment
Question by:campinas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 28

Expert Comment

by:AzraSound
ID: 11698801
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
 

Author Comment

by:campinas
ID: 11698948
In my case the parent is the same.
0
 
LVL 28

Assisted Solution

by:AzraSound
AzraSound earned 1000 total points
ID: 11700336
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 3

Expert Comment

by:TILL
ID: 11715726
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
 

Author Comment

by:campinas
ID: 11716449
TILL, Previous and Next properties are read-only.
0
 
LVL 3

Accepted Solution

by:
TILL earned 1000 total points
ID: 11716667
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
 

Author Comment

by:campinas
ID: 11732879
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month13 days, 6 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question