Visual Basic Common Control API Routines
Pure VB: Duplicating the Contents of a TreeView
Posted:   Wednesday June 28, 1998
Updated:   Monday December 26, 2011
Applies to:   VB4-32, VB5, VB6
Developed with:   VB5, Windows 98
OS restrictions:   None
Author:   Brad Martinez
An existing project with a populated TreeView.

Brad Martinez made this copy routine available via the newsgroups. 

The three routines below combine to produce an effective way to duplicate a TreeView with all its data. No API is used, yet the method executes quite quickly. To maximize speed when copying a large TreeView, set the receiving TreeView's visible property to False before beginning the copy.

The method below assumes you've placed a second TreeView on the form for populating with the contents of the first. It is certainly possible however to use the Load statement to create a control array of TreeViews if necessary.

 BAS Module Code

 Form Code
Add a command button to start the copying method, and a second TreeView (Treeview2) to receive the data. Add the following three routines to a form containing a TreeView control:

Option Explicit
' Copyright 1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
'Brad Martinez,

Private Sub Command1_Click()

   CopyTreeview TreeView1, TreeView2

End Sub

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
   For Each nodeRoot In objTVSrc.Nodes
      If (nodeRoot.Parent Is Nothing) Then
         Call CopyTVParentNode(nodeRoot, objTVDest.Nodes)
      End If

End Sub

Private Sub CopyTVParentNode(nodeParent As Node, nodesDest As Nodes)

  'Walks the specified source parent treeview node,
  'and all of its 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 its own children...
      If nodeChild.Children Then
        'Recursively call this proc copying all of its children
        '(it becomes the new parent node)
         Call CopyTVParentNode(nodeChild, nodesDest)
        '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
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
Run the project and populate your treeview as usual (Treeview1). press the command button to copy all Treeview1 nodes to Treeview2.


PayPal Link
Make payments with PayPal - it's fast, free and secure!


Copyright 1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy


Hit Counter