Results 1 to 4 of 4

Thread: Copy a Treeveiw Control

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 1999
    Location
    UK
    Posts
    25

    Exclamation

    Can anyone help me with this small problem.

    I have a treeview control on one form which at runtime the user can add items to it. The user then click a button to display another form. I need to copy the treeview from the first form and display it on the second form.

    Is there a very simple way to do this.

    Thanks

  2. #2
    Addicted Member S@NSIS's Avatar
    Join Date
    Aug 2000
    Location
    Stoke-On-Trent, England
    Posts
    243
    Hi,
    You might want to take a look at this excellent piece of code which was originally posted by Joacim Andersson:

    http://www.vb-world.net/tips/tip498.html

    Hope this helps

    Shaun
    Web/Application Developer
    VB6 Ent (SP5), Win 2000,SQL Server 2000

  3. #3
    Addicted Member S@NSIS's Avatar
    Join Date
    Aug 2000
    Location
    Stoke-On-Trent, England
    Posts
    243

    Thumbs up

    This is a better way of doing it!

    Code:
    Option Explicit
    
    Private Sub CopyTreeview(objTVSrc As TreeView, objTVDest As TreeView)
    
    'Copies the source treeview to a destination
    'treeview. Assumes that both treeviews use the
    'same (or identical) Imagelists.
    
        Dim nodeRoot As Node
    
        objTVDest.Nodes.Clear
    
        For Each nodeRoot In objTVSrc.Nodes
            If (nodeRoot.Parent Is Nothing) Then
                Call CopyTVParentNode(nodeRoot, objTVDest.Nodes)
            End If
        Next
    
    End Sub
    
    
    Private Sub CopyTVParentNode(nodeParent As Node, nodesDest As Nodes)
    
    'Walks the specified source parent treeview node,
    'and all of it's children nodes, adding them to the
    'specified destination Nodes collection.
    '
    'nodeParent: parent node to walk and copy from
    'nodesDest : destination Nodes collection to copy to
    
        Dim nodeDummy As Node
        Dim nodeChild As Node
    
    'First add the parent node to the destination nodes collection.
        Set nodeDummy = CopyNode(nodeParent, nodesDest)
    
    'Get the current parent node's first child
        Set nodeChild = nodeParent.Child
    
    'Now walk through the current parent node's children
    'appending the current child node's text to the passed string
        Do While Not (nodeChild Is Nothing)
    
    'If the current child node has it's own children...
            If nodeChild.Children Then
    
    'Recursively call this proc copying all of it's children
    '(it becomes the new parent node)
                Call CopyTVParentNode(nodeChild, nodesDest)
    
            Else
    
    'Add the child node to the destination nodes collection.
                Set nodeDummy = CopyNode(nodeChild, nodesDest)
    
            End If
    
    'Get the current child node's next sibling
            Set nodeChild = nodeChild.Next
    
        Loop
    
    End Sub
    
    
    Private Function CopyNode(nodeSrc As Node, nodesDest As Nodes) As Node
    
        With nodeSrc
    
            If (.Parent Is Nothing) Then   'Root node
    
                Set CopyNode = nodesDest.Add(, , _
                        .Key, .Text, _
                        .Image, .SelectedImage)
                CopyNode.Expanded = True
    
            Else   'Child node
    
                Set CopyNode = nodesDest.Add(.Parent.Index, _
                        tvwChild, _
                        .Key, .Text, _
                        .Image, .SelectedImage)
                CopyNode.Expanded = True
    
            End If
    
        End With
    
    End Function
    
    'Use like this
    
    Private Sub Command1_Click()
    
        CopyTreeview TreeView1, TreeView2
    
    End Sub
    Hope this helps

    Shaun
    Web/Application Developer
    VB6 Ent (SP5), Win 2000,SQL Server 2000

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Oct 1999
    Location
    UK
    Posts
    25

    Thanks

    Shaun,

    Nice bit iof code, thanks

    Phil.

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