2
votes

I have been trying to search the web for information regarding child nodes, but I seem to find everything but what I'm looking for.

Basically, my problem is this: I have determined if a parent node is checked in my treeview control. What I need to do is loop through all child nodes of this parent to determine what children are checked and then load these into an array.

As I already know the parent node, I am hoping I don't have to loop through all nodes again. I want to just loop through all child nodes of a specified parent.

Hope that makes sense.

My Treeview looks similar to this:

Name
  -->Name 1
  -->Name 2
  -->Name 3
  -->Etc
Code
  -->Code 1
  -->Code 2
  -->Code 3
  -->Etc

So my example would look like this:

 If trvFilter.Nodes.Item(trvFilter.Nodes.IndexOfKey("Name")).Checked = True Then
             'Loop through Child Nodes of Parent Node (Name)
             'If Child Node is checked, add the name of the Child node to an array
         End If

Sorry if this seems simple, but I'm new to vb.net and can't work out how to loop through the child nodes of a selected parent.

Thanks in advance

2

2 Answers

1
votes

I seem to have figured out a method of getting my end results that I required with the following code:

If trvFilter.Nodes.Item(trvFilter.Nodes.IndexOfKey("Name")).Checked = True Then
            'Loop through Child Nodes of Parent Node (Name)
            For Each cn As TreeNode In trvFilter.Nodes.Item(trvFilter.Nodes.IndexOfKey("Name")).Nodes
                'If Child Node is checked, add the name of the Child node to an array
                If cn.Checked Then
                    StoreRecordArray.Add(cn.Text)
                End If
            Next
        End If

Please feel free to update my code if there is an easier (tidier) method. Thanks

0
votes

trvFilter.Nodes is a collection of TreeNode, so you can loop through them with a for-each

for each tvn as TreeNode in trvFilter.Nodes

 ' do stuff with tvn

next