How do I remove nodes from a treeview

I am using the attached code snippet to remove nodes from a treeview. The tree contains a scan of a PC showing all folders containing exe files on the machine. It also shows all other folders which I want to remove from the tree.

However, it only removes 1 such node them terminates (the search not the program).

Can someone tell me what i am doing wrong?

Thank you
Private Sub FindAndRemoveNode()
        Dim nodes As TreeNodeCollection = TreeView1.Nodes
        Dim n As TreeNode
        For Each n In nodes
            FindRecursiveRemoveNode(n)
            dlgProgress.ProgressBar1.PerformStep()
        Next
    End Sub
 
     Private Sub FindRecursiveRemoveNode(ByVal tNode As TreeNode)
        Dim tn As TreeNode
        For Each tn In tNode.Nodes
            dlgProgress.tbFileName.Text = tn.Text.ToString()
            dlgProgress.ProgressBar1.PerformStep()
            If InStr(tn.Text, ".exe") Then
                FindRecursiveRemoveNode(tn)
            Else
                tn.Remove()
                FindRecursiveRemoveNode(tn)
            End If
        Next
    End Sub

Open in new window

LVL 1
jindaleeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ToddBeaulieuCommented:
Taking a quick glance at your code, I don't think you're working your way up the tree. Your recursive method should, given a node, remove all child nodes that have no children themselves and call itself on any child nodes that have further child nodes. It seems you're removing a node from the tree and then trying to recurse down into it, after it's gone. I'd process then remove.
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Try this out...
    Private Sub FindAndRemoveNode()
        Dim RemoveList As New List(Of TreeNode)
 
        dlgProgress.ProgressBar1.Style = ProgressBarStyle.Marquee
        For Each n As TreeNode In TreeView1.Nodes
            FindRecursiveRemoveNode(n, RemoveList)
            Application.DoEvents()
        Next
 
        For Each n As TreeNode In RemoveList
            n.Remove()
        Next
        dlgProgress.ProgressBar1.Style = ProgressBarStyle.Blocks
    End Sub
 
    Private Sub FindRecursiveRemoveNode(ByVal tNode As TreeNode, ByVal RemoveList As List(Of TreeNode))
        dlgProgress.tbFileName.Text = tNode.Text
        Application.DoEvents()
        If tNode.Text.ToLower.IndexOf(".exe") <> -1 Then
            RemoveList.Add(tNode)
        Else
            For Each n As TreeNode In tNode.Nodes
                FindRecursiveRemoveNode(n, RemoveList)
            Next
        End If
    End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jindaleeAuthor Commented:
Worked a treat..

Thank you!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.