• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 613
  • Last Modified:

Positioning TreeView Nodes

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
Egore
Asked:
Egore
  • 4
  • 4
  • 3
  • +1
1 Solution
 
ramsesCommented:
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
 
ramsesCommented:
disregard the tree in the comment above
0
 
EgoreAuthor Commented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ramsesCommented:
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
 
Julian_KCommented:
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
 
Julian_KCommented:
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
 
Julian_KCommented:
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
 
deightonCommented:
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
 
Julian_KCommented:
hmmm.... easier? Try writing some code and than compare with mine. You might be right, I don't know :-)
0
 
EgoreAuthor Commented:
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
 
EgoreAuthor Commented:
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
 
EgoreAuthor Commented:
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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now