CodeItBetter Programming Another VB Programming Blog

How to copy the content of one Tree View to another Tree View

Posted on January 5, 2009
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
'Controls - How to copy the content of one Tree View to another Tree View

Option Explicit
 
'Add two Tree View Controls to your form, and a Command Button.

Private Sub Command1_Click()
    'The Command below will copy the content of TreeView1 to Treeview2
    CopyTreeview TreeView1, TreeView2
End Sub
 
Private Sub CopyTreeview(objTVSrc As TreeView, objTVDest As TreeView)
    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 nodeRoot
End Sub
 
Private Sub CopyTVParentNode(nodeParent As Node, nodesDest As Nodes)
    Dim nodeDummy As Node, nodeChild As Node
    Set nodeDummy = CopyNode(nodeParent, nodesDest)
    Set nodeChild = nodeParent.Child
    Do While Not (nodeChild Is Nothing)
        If nodeChild.Children Then
            Call CopyTVParentNode(nodeChild, nodesDest)
        Else
            Set nodeDummy = CopyNode(nodeChild, nodesDest)
        End If
        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
            Set CopyNode = nodesDest.Add(, , .Key, .Text, .Image, .SelectedImage)
            CopyNode.Expanded = True
        Else
            Set CopyNode = nodesDest.Add(.Parent.Index, _
                                         tvwChild, .Key, .Text, .Image, .SelectedImage)
            CopyNode.Expanded = True
        End If
    End With
End Function
 
Private Sub Form_Load()
    TreeView1.Nodes.Add , , "Sample", "Primary"
    TreeView1.Nodes.Add , , "Sample2", "Primary2"
    TreeView1.Nodes.Add "Sample", tvwChild, "Sample3", "Child"
End Sub
Filed under: Controls Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.