Solved

VB.net TreeView Gather checked nodes

Posted on 2014-12-28
2
758 Views
Last Modified: 2014-12-28
Hi

I have TreeView with the CheckBoxes property set to True
After the user has checked various nodes, what VB.net code
would I use to gather the text of only these checked nodes?
0
Comment
Question by:murbro
2 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 40520519
You need a recursive routine to traverse the nodes collections of the treeview and gather the checked nodes.

Here's one possibility:
Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim checked As List(Of TreeNode) = GetCheckedNodes(TreeView1)
        For Each node As TreeNode In checked
            Debug.Print(node.FullPath)
        Next
    End Sub

    Private Function GetCheckedNodes(ByVal TV As TreeView) As List(Of TreeNode)
        Dim checked As New List(Of TreeNode)
        For Each node As TreeNode In TV.Nodes
            GetCheckedNodes(node, checked)
        Next
        Return checked
    End Function

    Private Sub GetCheckedNodes(ByVal tn As TreeNode, ByVal checked As List(Of TreeNode))
        If tn.Checked Then
            checked.Add(tn)
        End If
        For Each childNode As TreeNode In tn.Nodes
            GetCheckedNodes(childNode, checked)
        Next
    End Sub

End Class

Open in new window

0
 

Author Closing Comment

by:murbro
ID: 40521379
Thanks very much
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question