Solved

Re-order Nodes in TreeView

Posted on 2004-08-02
7
1,056 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 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

724 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