J
Jonathan Brown
I've been able to populate a treeview control on a userform with several
nodes in a hierarchical fashion. Here's an example of what it might contain:
*It's supposed to be showing the indents for CONUS and OCONUS under the
names and the sites are supposed to be indented under them. You're supposed
to see three levels of hierarchy here but it's not showing it.
Peter Masson
CONUS
Site 1
Site 2
Site 3
OCONUS
Site 4
Site 5
Site 6
Marti Mingo
CONUS
Site 7
Site 8
Site 9
OCONUS
Site 10
Site 11
Site 12
Steve Crapo
CONUS
Site 13
Site 14
Site 15
Site 16
....
Anyway, I'm trying to figure out how I can program an up button and a down
button that would allow me to move a node in the treeview up or down in the
list. I've been able to get it to move a node within it's own level within
the heirarchy but if the node contains child nodes it loses information found
in the key propery for each node. If I attempt to move Steve Crapo between
Marti Mingo and Peter Masson, then it'll move all the child nodes with it,
which is great and precisely what I want, but they all lose the values in
their Key Properties as well as the information found in their Parent
properties too.
Here's the code that I'm using:
Code:
Private Sub cmdDown_Click()
MoveNode Me.tvFilter, Me.tvFilter.selecteditem, "DOWN"
End Sub
Private Sub cmdUp_Click()
MoveNode Me.tvFilter, Me.tvFilter.selecteditem, "UP"
End Sub
Sub MoveNode(tvw As TreeView, OriginalNode As node, Direction As String)
Dim NewNode As node
Dim strKey As String
Dim boolChecked As Boolean
Dim boolExpanded As Boolean
'All we do here is copy the node and set it as the previous
'Nodes previous node. A little confusing, but it works.
'We then add all the children and delete the original
'Node
With tvw
Select Case Direction
Case "UP"
If Not OriginalNode.Previous Is Nothing Then
Set NewNode = .Nodes.Add(OriginalNode.Previous, tvwPrevious, ,
OriginalNode.Text)
Else
Exit Sub
End If
Case "DOWN"
If Not OriginalNode.Next Is Nothing Then
Set NewNode = .Nodes.Add(OriginalNode.Next, tvwNext, ,
OriginalNode.Text)
Else
Exit Sub
End If
End Select
NewNode.Selected = True
If OriginalNode.Children <> 0 Then
GetChildren tvw, OriginalNode, NewNode
End If
strKey = OriginalNode.Key
boolChecked = OriginalNode.Checked
boolExpanded = OriginalNode.Expanded
.Nodes.Remove OriginalNode.index
Set OriginalNode = Nothing
NewNode.Key = strKey
NewNode.Checked = boolChecked
NewNode.Expanded = boolExpanded
NewNode.Selected = True
End With
End Sub
Private Sub GetChildren(tvw As TreeView, OriginalNode As node, NewNode As
node)
Dim FirstChildNode As node
Dim NextChildNode As node
Dim i As Integer
With tvw
'For each child in the tree
For i = 1 To OriginalNode.Children
'If it's the first child:
If i = 1 Then
'Add the node:
Set FirstChildNode = .Nodes.Add(NewNode.index, tvwChild, ,
OriginalNode.Child.Text)
'Set us up for the next child:
Set NextChildNode = OriginalNode.Child.Next
'Get the added nodes children:
If OriginalNode.Child.Children <> 0 Then
GetChildren tvw, OriginalNode.Child, FirstChildNode
End If
'It's not the first child:
Else
On Error Resume Next
'Add the node:
Set FirstChildNode = .Nodes.Add(NewNode.index, tvwChild, ,
NextChildNode.Text)
'Get the added nodes children:
If NextChildNode.Children <> 0 Then
GetChildren tvw, NextChildNode, FirstChildNode
End If
'Set us up again:
Set NextChildNode = NextChildNode.Next
End If
Next
End With
End Sub
Due to the fact that the key properties have to be unique, I can't create
the new nodes before I delete the old ones which in this case is what's
happening. I'm guessing what I have to do is find all the child nodes and
load them into an array containing the node text, parent property, key
property, and I'd also like to keep whether they're checked and expanded. I
then need to delete the old nodes and then create the new nodes reading from
the array and assigning the properties back into it.
I have the hardest time wrapping my head around the concepts of iteration. I
would love some help on this from anyone.
Also, you can see that at the end of the MoveNode() procedure the parent's
Key property (as well as checked and expanded properties) is maintained, it's
just that the children's Key property's are not as evidenced by the code in
the GetChildren Procedure.
Thanks!
nodes in a hierarchical fashion. Here's an example of what it might contain:
*It's supposed to be showing the indents for CONUS and OCONUS under the
names and the sites are supposed to be indented under them. You're supposed
to see three levels of hierarchy here but it's not showing it.
Peter Masson
CONUS
Site 1
Site 2
Site 3
OCONUS
Site 4
Site 5
Site 6
Marti Mingo
CONUS
Site 7
Site 8
Site 9
OCONUS
Site 10
Site 11
Site 12
Steve Crapo
CONUS
Site 13
Site 14
Site 15
Site 16
....
Anyway, I'm trying to figure out how I can program an up button and a down
button that would allow me to move a node in the treeview up or down in the
list. I've been able to get it to move a node within it's own level within
the heirarchy but if the node contains child nodes it loses information found
in the key propery for each node. If I attempt to move Steve Crapo between
Marti Mingo and Peter Masson, then it'll move all the child nodes with it,
which is great and precisely what I want, but they all lose the values in
their Key Properties as well as the information found in their Parent
properties too.
Here's the code that I'm using:
Code:
Private Sub cmdDown_Click()
MoveNode Me.tvFilter, Me.tvFilter.selecteditem, "DOWN"
End Sub
Private Sub cmdUp_Click()
MoveNode Me.tvFilter, Me.tvFilter.selecteditem, "UP"
End Sub
Sub MoveNode(tvw As TreeView, OriginalNode As node, Direction As String)
Dim NewNode As node
Dim strKey As String
Dim boolChecked As Boolean
Dim boolExpanded As Boolean
'All we do here is copy the node and set it as the previous
'Nodes previous node. A little confusing, but it works.
'We then add all the children and delete the original
'Node
With tvw
Select Case Direction
Case "UP"
If Not OriginalNode.Previous Is Nothing Then
Set NewNode = .Nodes.Add(OriginalNode.Previous, tvwPrevious, ,
OriginalNode.Text)
Else
Exit Sub
End If
Case "DOWN"
If Not OriginalNode.Next Is Nothing Then
Set NewNode = .Nodes.Add(OriginalNode.Next, tvwNext, ,
OriginalNode.Text)
Else
Exit Sub
End If
End Select
NewNode.Selected = True
If OriginalNode.Children <> 0 Then
GetChildren tvw, OriginalNode, NewNode
End If
strKey = OriginalNode.Key
boolChecked = OriginalNode.Checked
boolExpanded = OriginalNode.Expanded
.Nodes.Remove OriginalNode.index
Set OriginalNode = Nothing
NewNode.Key = strKey
NewNode.Checked = boolChecked
NewNode.Expanded = boolExpanded
NewNode.Selected = True
End With
End Sub
Private Sub GetChildren(tvw As TreeView, OriginalNode As node, NewNode As
node)
Dim FirstChildNode As node
Dim NextChildNode As node
Dim i As Integer
With tvw
'For each child in the tree
For i = 1 To OriginalNode.Children
'If it's the first child:
If i = 1 Then
'Add the node:
Set FirstChildNode = .Nodes.Add(NewNode.index, tvwChild, ,
OriginalNode.Child.Text)
'Set us up for the next child:
Set NextChildNode = OriginalNode.Child.Next
'Get the added nodes children:
If OriginalNode.Child.Children <> 0 Then
GetChildren tvw, OriginalNode.Child, FirstChildNode
End If
'It's not the first child:
Else
On Error Resume Next
'Add the node:
Set FirstChildNode = .Nodes.Add(NewNode.index, tvwChild, ,
NextChildNode.Text)
'Get the added nodes children:
If NextChildNode.Children <> 0 Then
GetChildren tvw, NextChildNode, FirstChildNode
End If
'Set us up again:
Set NextChildNode = NextChildNode.Next
End If
Next
End With
End Sub
Due to the fact that the key properties have to be unique, I can't create
the new nodes before I delete the old ones which in this case is what's
happening. I'm guessing what I have to do is find all the child nodes and
load them into an array containing the node text, parent property, key
property, and I'd also like to keep whether they're checked and expanded. I
then need to delete the old nodes and then create the new nodes reading from
the array and assigning the properties back into it.
I have the hardest time wrapping my head around the concepts of iteration. I
would love some help on this from anyone.
Also, you can see that at the end of the MoveNode() procedure the parent's
Key property (as well as checked and expanded properties) is maintained, it's
just that the children's Key property's are not as evidenced by the code in
the GetChildren Procedure.
Thanks!