Solved

Re-order Nodes in TreeView

Posted on 2004-08-02
7
1,043 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now