Solved

Re-order Nodes in TreeView

Posted on 2004-08-02
7
1,051 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
  • 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 250 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
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
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 250 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA saving file message display 5 67
Reading the Contents of a Directory In Access VBA 5 70
using Access 8 68
VBA/SQL - Connect to SQL server and pull data 4 111
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

803 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