Click here to Skip to main content
15,885,876 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
The following code populates a treeview control from a XML file. In the treeview control each node has a check box. How can I get all the selected (checkbox.checked = true) nodes value?
Something like
For all node.checkbox.checked = true then
Msgbox node.value
next
Dim recordNode As TreeNode
        Dim leaderNode As TreeNode
        Dim controlfieldNode As TreeNode
        Dim datafielNode As TreeNode

        For Each Record As XElement In doc...<record>
            recordNode = TreeView1.Nodes.Add(Record.Name.ToString)
            For Each Leader As XElement In Record...<leader>
                leaderNode = recordNode.Nodes.Add(Leader.Name.ToString)
                For Each Controlfield As XElement In Record...<controlfield>
                    controlfieldNode = recordNode.Nodes.Add(Controlfield.@tag.ToString)
                    For Each Datafield As XElement In Record...<datafield>
                        datafielNode = recordNode.Nodes.Add(Datafield.@tag.ToString)
                        For Each Subfield As XElement In Datafield...<subfield>
                            datafielNode.Nodes.Add(Subfield.@code.ToString)

                        Next
                    Next
                Next
            Next
        Next
Posted
Updated 10-May-11 9:56am
v2

1 solution

You need to create a list to hold the checked nodes and call a function recursively. Something like:

Sub CheckedNodes(list as List(Of TreeNode), currentnode as TreeNode)

  For Each node as TreeNode in currentnode.Nodes
    If node.Checked Then list.Add(node)
    CheckedNodes(list, node)
  Next

End Sub
 
Share this answer
 
Comments
stopete82 11-May-11 12:04pm    
Once my treeview1 gets loaded from the XML file; I checked some checks boxes and then click on the fallowing button so I can output the name of the node that was checked. However, I get empty strings. Appreciate your help.

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

Dim list As New List(Of String)
GetSelectedNodes(TreeView1.Nodes, list)

End Sub

Private Sub GetSelectedNodes(ByVal nodes As TreeNodeCollection, ByRef list As List(Of String))
For Each node As TreeNode In nodes
If node.Checked Then list.Add(node.Name.ToString)
GetSelectedNodes(node.Nodes, list)

Next

' Loop through list with a for to loop.
Dim i As Integer
For i = 0 To list.Count - 1
MsgBox(list.Item(i))
Next i


End Sub
stopete82 11-May-11 13:05pm    
Thanks Fabio V Silva for the great idea. This is the code that worked for me. Thanks again

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

For Each n As TreeNode In GetCheck(TreeView1.Nodes)
MsgBox(n.Text)
Next


End Sub

Private Function GetCheck(ByVal node As TreeNodeCollection) As List(Of TreeNode)

Dim lN As New List(Of TreeNode)
For Each n As TreeNode In node
If n.Checked Then lN.Add(n)
lN.AddRange(GetCheck(n.Nodes))
Next

Return lN

End Function
Fabio V Silva 11-May-11 13:39pm    
Your welcome. Sorry, I couldn't reply to your other post earlier.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900