How to get a List of Checked items from a Treeview

  • Thread starter Thread starter Jack
  • Start date Start date
J

Jack

Hello,

I have a treeview with checkboxes and I want to get the Text of each checked
item into an arraylist or something.

Thanks,

Jack
 
Hi,

Use recursion to get a list of the checked nodes.

VB.Net 2003

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)
Trace.WriteLine(n.Text)
Next
End Sub

Private Function GetCheck(ByVal node As TreeNodeCollection) As ArrayList
Dim lN As New ArrayList
For Each n As TreeNode In node
If n.Checked Then lN.Add(n)
lN.AddRange(GetCheck(n.Nodes))
Next

Return lN
End Function

VB.Net 2005 method

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)
Trace.WriteLine(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

Ken
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top