Solved

Positioning TreeView Nodes

Posted on 2001-09-06
12
572 Views
Last Modified: 2007-12-19
I'm using VB6.0 with the TreeView control.  I'm trying to figure out how I can move a node to a specific location in the TreeView control.  I know how to make a node become a child of another node by setting the parent property, but is it possible to then make that node appear as, say, the third child of the node, instead of always becoming the first (topmost) child?

Thanks,
Alex
0
Comment
Question by:Egore
  • 4
  • 4
  • 3
  • +1
12 Comments
 
LVL 2

Expert Comment

by:ramses
Comment Utility
You can only do that if you have that many children nodes.

Let me explain.

Consider the following tree

Root
  Item 1
    sub 1
    sub 2
    sub 3
  Item 2
    sub 1
      subchild 1
      subchild 2
      subchild 3
    sub 2

Use these constants for relationship when creating nodes

tvwChild
tvwFirst
tvwLast
tvwNext


this determines if a node you create is child, first, last of next

If this does not provide enough explanation then please give us some more info    



Ramses
0
 
LVL 2

Expert Comment

by:ramses
Comment Utility
disregard the tree in the comment above
0
 
LVL 3

Author Comment

by:Egore
Comment Utility
I have been able to create nodes wherever I need them, but my problem is, once created, how can I move them to a specific location?

- Alex
0
 
LVL 2

Expert Comment

by:ramses
Comment Utility
Moving is often accomplished like this:

Copy source in memory
Delete source
Create object where you want it
Paste memory to object.

Meaning, copy the node, delete it, and re-create it on the new location.


Ramses
0
 
LVL 5

Accepted Solution

by:
Julian_K earned 100 total points
Comment Utility
It depends what you want to do.
If you're trying to sort the child nodes on a parent node, every time you move/add a node to the parent, execute

"objParentNode.Sorted=true"

If you use some kind of custom sort, that does not applies to the previous, you have two choices:

1.
 a)remove the node from the treeview (if it is already added and you just want to move it to another place)
 b)add it, using "tvwNext" relationship:
  Treeview1.Nodes.Add objPrevNode.key, tvwNext, <somekey>, <sometext>

2.
 a)add the child node in the common way:
   "objChildNode.parent=objParentNode"
 b)then use the following sub like this: SortOneNode(objChildNode)

Private Sub SortOneNode(ByVal MovedNode As Node)
        Dim NextNode As Node
   
    Do Until (MovedNode.Next Is Nothing)
        'get the next node
        Set NextNode = MovedNode.Next
       
        'here you can compare the nodes to determine
        'if you should move it down further
        'using (True) will move the node to the bottom.
        If (True) Then
            'move the node one step down
            Set tmpNextNode.Parent = tmpNode.Parent
            Set tmpNextNode = tmpNode.Next
        Else
            'stop moving
            Exit Do
        End If
    Loop
End Sub
0
 
LVL 5

Expert Comment

by:Julian_K
Comment Utility
Oops...
"2.a)add the child node in the common way:"
must be
"2.a)move the child node in the common way:"

...
I think this will help.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 5

Expert Comment

by:Julian_K
Comment Utility
And again... (i was in a hurry and made some mistakes), here's the working code:


Private Sub SortOneNode(ByVal MovedNode As Node)
        Dim NextNode As Node

    'start looping through the next nodes...
    Do Until (MovedNode.Next Is Nothing)

        'get the next node
        Set NextNode = MovedNode.Next
       
        'here you can compare the nodes to determine
        'if you should move it down further
        'using (True) will move the node to the bottom.
        If (True) Then
            'move the node one step down
            Set NextNode.Parent = MovedNode.Parent
            Set NextNode = MovedNode.Next
        Else
            'stop moving
            Exit Do
        End If

    Loop

    'clear the objects
    Set NextNode = Nothing
End Sub
0
 
LVL 18

Expert Comment

by:deighton
Comment Utility
how about make a new node below the parent node as a new  node

move the nodes of the old parent to the new parent in the order you want them (by setting the parent property of the new parent)

delete old parent

set key of new parent to that of the old parent.


-----

Should be an easier way though.
0
 
LVL 5

Expert Comment

by:Julian_K
Comment Utility
hmmm.... easier? Try writing some code and than compare with mine. You might be right, I don't know :-)
0
 
LVL 3

Author Comment

by:Egore
Comment Utility
Julian K,

Geez, I had thought about doing something like that, but I had assumed that the TreeView control in all its splendor would have that built in...  Dang it all.

I see what you're doing with that code, and it's conceptually rather ugly, but if that's the only way, then I guess I'll do it.

deighton,

Yeah, that's the first step in the process, but then I have to go through all of that rigamorale that Julian K mentioned.

Now, if only the TreeView were open source then this could be built in :-)  Heh, anyway, thanks everyone!

- Alex
0
 
LVL 3

Author Comment

by:Egore
Comment Utility
deighton,

My fault, you're right, that is slightly different than Julian suggested.  But that is actually going to end up being more difficult than Julian's method, I think.  I really just need to be able to move a node up or down within a group, I have drag-and-drop capabilities for moving the node to a new parent, so Julian's method works rather well.

- Alex
0
 
LVL 3

Author Comment

by:Egore
Comment Utility
I don't know if any of you want this, but this is the code that I ended up going with to move a node up or down in a tree.  It is based closely on Julian K's answer.

Private Sub MoveNode(NodeToMove As Node, Distance As Integer)

    Dim I As Integer
    Dim TargetNode As Node
    Dim Down As Boolean
   
    ' Distance is negative for moving up, positive for moving down
    If Distance = Abs(Distance) Then Down = True Else Down = False
    Distance = Abs(Distance)
   
    ' Determine our starting point
    If Down Then
        Set TargetNode = NodeToMove.Next
    Else
        Set TargetNode = NodeToMove
    End If
   
    ' Determine what node comes directly after this node once the move is done
    For I = 1 To Distance
        ' You can only move so far in any given direction
        If TargetNode Is Nothing Then Exit For
       
        If Down Then
            Set TargetNode = TargetNode.Next
        Else
            Set TargetNode = TargetNode.Previous
        End If
    Next
   
    ' Move node up to the top of the sibling list
    Set NodeToMove.Parent = NodeToMove.Parent
   
    ' If we hit "Nothing" while moving up, then we're already at the stopping point...
    If Down Or Not TargetNode Is Nothing Then
        ' Loop through all lower nodes until we reach the designated node
        If TargetNode Is Nothing Then
            Do Until NodeToMove.LastSibling Is NodeToMove
                Set NodeToMove.LastSibling.Parent = NodeToMove.Parent
            Loop
        Else
            Do Until TargetNode.Previous Is NodeToMove
                Set TargetNode.Previous.Parent = NodeToMove.Parent
            Loop
        End If
    End If
   
    ' Clean up after ourselves
    Set TargetNode = Nothing
   
End Sub
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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…
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…

772 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

10 Experts available now in Live!

Get 1:1 Help Now