tags:

views:

34

answers:

1

How do I display Items that are Checked from a treeView in a listBox using VB?

A: 

Assuming that it's winforms:

ListView1.Items.Clear()
For Each node As TreeNode In TreeView1.Nodes
    If node.Checked Then
         ListView1.Items.Add(node.Text)
    End If
Next

Edit: Code for calling recursive method:

ListView1.Items.Clear()
AddToList(TreeView1.Nodes)

Recursive method:

private sub AddToList(nodes as TreeNodeCollection)
  For Each node As TreeNode In nodes
    If node.Checked Then
      ListView1.Items.Add(node.Text)
      AddToList(node.Nodes)
    End If
  Next
End Sub

You'd have to adjust this if you want them to appear in a certain order or anything like that. Can't remember if you might also have to have a check for 'If nodes is nothing Then return' at the beginning of the 'AddToList' method.

ho1
Thanks!!! It pulled my information sucessfully. Would I intergrate my subfolders and files in the same code?
jpavlov
@jpavlov: If you just want everything on the same level, just look at my adjusted answer.
ho1