Click to See Complete Forum and Search --> : Treeview into Array


leojunes
April 23rd, 2001, 04:56 AM
Hi all,
Pls. someone tell me how to put into array all the nodes of treeview


Example

Root
Node1
Node3
Node4
Node2
Node5
Node6


Output Array will be like this
Root Node1
Root Node2
Root Node3
Root Node4
Root Node5
Root Node6
Node1 Node3
Node1 Node4
and so on




Note that
Node1 has Node3 and Node4 only because that is only his children.


Thanks

Cakkie
April 23rd, 2001, 05:20 AM
I would just create an array of node objects. The node objects also contain all the information you need about parents:


' the form load fills a treeview
' the command1_click fill the array
' the command2_click shows the content of the array

private myArray() as Node

private Sub Command1_Click()

for t = 1 to TreeView1.Nodes.Count
ReDim Preserve myArray(t)
set myArray(t - 1) = TreeView1.Nodes(t)
next t

End Sub

private Sub Command2_Click()

Dim msg as string
Dim par as Node

msg = ""
for t = 0 to UBound(myArray)
on error resume next
Err.Clear
set par = nothing
set par = myArray(t).Parent
If Err.Number <> 0 Or par is nothing then ' parent does not exist
msg = msg & "root" & " - " & myArray(t).Key & vbCrLf
else
msg = msg & myArray(t).Parent.Key & " - " & myArray(t).Key & vbCrLf
End If
next t

MsgBox msg

End Sub

private Sub Form_Load()

' add some nodes to the treeview
for t = 1 to 5
TreeView1.Nodes.Add , , "Node" & t, "Node" & t
next t

' add 2 children to Node1
TreeView1.Nodes.Add "Node1", tvwChild, "Node6", "Node6"
TreeView1.Nodes.Add "Node1", tvwChild, "Node7", "Node7"

End Sub






Tom Cannaerts
slisse@planetinternet.be

Programming today is a race between software engineers striving to build bigger and better idot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook