# [?]TreeView-VB6



## integer (Jun 12, 2003)

Need help badlly with getting my the items in my TreeView1 to be listed in my ListView1 when checked in the TreeView1 control. I believe it can be done using :

```
Private Sub TreeView1_NodeCheck(ByVal Node As MSComctlLib.Node)
End Sub
```
However I am new to vb and programing and dont really understand any of what I am doing.

Here is my source so far :


```
Private Sub Form_Load()

TreeView1.Checkboxes = True
TreeView1.Nodes.Add , , "Root", "Root"

TreeView1.Nodes.Add "Root", tvwChild, "Child1", "Item"
TreeView1.Nodes.Add "Root", tvwChild, "Child2", "Item"
TreeView1.Nodes.Add "Root", tvwChild, "Child3", "Item"
TreeView1.Nodes.Add "Root", tvwChild, "Child4", "Item"
TreeView1.Nodes.Add "Root", tvwChild, "Child5", "Item"
TreeView1.Nodes.Add "Root", tvwChild, "Child6", "Item"
TreeView1.Nodes.Add "Root", tvwChild, "Child7", "Item"
TreeView1.Nodes(1).Expanded = True
End Sub


' ENulerate the children of a checked node
'
Private Sub TreeView1_NodeCheck(ByVal Node As MSComctlLib.Node)
Dim NodeNext, NodeLast, Child
Dim nodx As Node

'    MsgBox "Node has " & Node.Children & " Children."
    
    If Node.Children <> 0 Then
        Child = Node.Child.Key
    End If
    
    If Node.Checked = True Then
        ss = "True"
    Else
         ss = "False"
    End If
If Node.Children <> 0 Then
    With TreeView1
        NodeNext = .Nodes(Child).FirstSibling.Key
        NodeLast = .Nodes(Child).LastSibling.Key
        Do While (NodeLast = NodeLast)
            
             .Nodes(NodeNext).Checked = ss
             Set nodx = .Nodes.Item(.Nodes(NodeNext).Index)
             If nodx.Children <> 0 Then
                  Call TreeView1_NodeCheck(nodx)
             End If
             
             If NodeNext = NodeLast Then Exit Do

             NodeNext = .Nodes(NodeNext).Next.Key
            .Nodes(NodeNext).Checked = ss
        Loop
    End With
ElseIf Node.Children = 0 Then
    Call parent_check(Node)
End If

If Node.Checked = False Then
        Call parent_check(Node)
    End If
    
End Sub
```


----------



## coderitr (Oct 12, 2003)

I don't see where you are attempting to add the children of the checked node to a listview control. Am I missing something?


----------



## integer (Jun 12, 2003)

No U didnt I just havent added the child nodes yet they will come later after i get things working properly


----------



## coderitr (Oct 12, 2003)

So what part of the code that you posted is not working?


----------



## integer (Jun 12, 2003)

ok here is what I have workedout up till now and Im stuck again:


```
' ENulerate the children of a checked node
'
Private Sub TreeView1_NodeCheck(ByVal Node As MSComctlLib.Node)
Dim NodeNext, NodeLast, Child
Dim nodx As Node
Dim ListObj As ListItem
'    MsgBox "Node has " & Node.Children & " Children."
    
    If Node.Children <> 0 Then
        Child = Node.Child.Key
    End If
    
    If Node.Checked = True Then
        ss = "True"
    Else
         ss = "False"
    End If
If Node.Children <> 0 Then
    With TreeView1
        NodeNext = .Nodes(Child).FirstSibling.Key
        NodeLast = .Nodes(Child).LastSibling.Key
        Do While (NodeLast = NodeLast)
            
             .Nodes(NodeNext).Checked = ss
             Set nodx = .Nodes.Item(.Nodes(NodeNext).Index)
             If nodx.Children <> 0 Then
                  Call TreeView1_NodeCheck(nodx)
             End If
             
             If NodeNext = NodeLast Then Exit Do

             NodeNext = .Nodes(NodeNext).Next.Key
            .Nodes(NodeNext).Checked = ss
        Loop
    End With
ElseIf Node.Children = 0 Then
    Call parent_check(Node)
End If

If Node.Checked = False Then
        Call parent_check(Node)
    End If
        Set ListObj = ListView1.ListItems.Add
End Sub
```
This is what this does: 
1st it adds an item to ListView1 when checked in treeview1. I now need to get the items to be removed from ListView1 when unchecked in TreeView1.

Basicly I want the ListView1 to respond to the effects of Treeview1, wheather the items in TreeView1 be (un)checked then in ListView1 these items need to be added or removed to ListView1....

I hope that made since


----------



## coderitr (Oct 12, 2003)

Why not just clear the listview. ListView1.ListItems.Clear

If you don't want to do that then can you set the key on the listview item to the same value as the key on the treeview item then ListView1.ListItems("key").Delete. (Not 100% sure on the syntax of that delete so check it.)


----------

