Solved

Positioning TreeView Nodes

Posted on 2001-09-06
12
600 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
[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
  • 4
  • 4
  • 3
  • +1
12 Comments
 
LVL 2

Expert Comment

by:ramses
ID: 6460639
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
ID: 6460642
disregard the tree in the comment above
0
 
LVL 3

Author Comment

by:Egore
ID: 6460687
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!

 
LVL 2

Expert Comment

by:ramses
ID: 6460711
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
ID: 6460753
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
ID: 6460767
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
 
LVL 5

Expert Comment

by:Julian_K
ID: 6460802
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
ID: 6460833
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
ID: 6460922
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
ID: 6460941
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
ID: 6460992
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
ID: 6461163
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

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!

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…

696 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