VB .NET Winforms loop through treeview nodes to delete empty nodes (nodes that do not contain child nodes with a certain tag).
Hello and thanks in advance for your help.

I have a treeview that is populated from a HDD folder collection. Its structure is similar to the following (however the structure is dynamic):

    My Disk:
    |
    |--folder1(tag:folder)
    |  |--subfolder1(tag:folder)
    |--folder2(tag:folder)
    |--folder3(tag:folder)
    |  |--subfolder1(tag:folder)
    |  |  |--file1(tag:file)
    |  |--subfolder2(tag:folder)
    |--folder4(tag:folder)
       |file1(tag:file)

   
I would like to delete all nodes from the tree that do not contain nodes with the tag "file" (basically empty folders). I would like the resulting tree structure to look like this:

    My Disk:
    |
    |--folder3(tag:folder)
    |  |--subfolder1(tag:folder)
    |     |--file1(tag:file)
    |--folder4(tag:folder)
       |file1(tag:file)

   
The best that I have come up with is the following:

    Private Sub deleteNode(byval nc as TreeNodeCollection)
     For Each tn As TreeNode In nc
      'delete node if it applies
            If tn.Tag = "folder" Then
                If tn.Nodes.Count = 0 Then
                     nc.Remove(tn)
                End If
            End If
    
            If tn.Nodes.Count > 0 Then
                 deleteNode(tn.Nodes)
            End If
        Next
    End Sub

I call the sub as follows:

    deleteNode(treeview1.Nodes(0).Nodes)

However, the above is not working properly. It deletes only some nodes and not all the targeted nodes. Could you please help me out by fixing my sub?

Many thanks
VB .NET Winforms loop through treeview nodes to delete empty nodes (nodes that do not contain child nodes with a certain tag).
===

Hello and thanks in advance for your help.

I have a treeview that is populated from a HDD folder collection. Its structure is similar to the following (however the structure is dynamic):

    My Disk:
    |
    |--folder1(tag:folder)
    |  |--subfolder1(tag:folder)
    |--folder2(tag:folder)
    |--folder3(tag:folder)
    |  |--subfolder1(tag:folder)
    |  |  |--file1(tag:file)
    |  |--subfolder2(tag:folder)
    |--folder4(tag:folder)
       |file1(tag:file)

   
I would like to delete all nodes from the tree that do not contain nodes with the tag "file" (basically empty folders). I would like the resulting tree structure to look like this:

    My Disk:
    |
    |--folder3(tag:folder)
    |  |--subfolder1(tag:folder)
    |     |--file1(tag:file)
    |--folder4(tag:folder)
       |file1(tag:file)

   
The best that I have come up with is the following:

    Private Sub deleteNode(byval nc as TreeNodeCollection)
     For Each tn As TreeNode In nc
      'delete node if it applies
            If tn.Tag = "folder" Then
                If tn.Nodes.Count = 0 Then
                     nc.Remove(tn)
                End If
            End If
    
            If tn.Nodes.Count > 0 Then
                 deleteNode(tn.Nodes)
            End If
        Next
    End Sub

I call the sub as follows:

    deleteNode(treeview1.Nodes(0).Nodes)

However, the above is not working properly. It deletes only some nodes and not all the targeted nodes. Could you please help me out by fixing my sub?

Many thanks