CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2000
    Location
    Manila Philippines
    Posts
    65

    Treeview into Array

    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




  2. #2
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: Treeview into Array

    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
    [email protected]

    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
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured