Solved

How do I remove nodes from a treeview

Posted on 2009-06-27
3
416 Views
Last Modified: 2013-11-27
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

0
Comment
Question by:jindalee
3 Comments
 
LVL 16

Expert Comment

by:ToddBeaulieu
ID: 24727223
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
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 24730229
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
 
LVL 1

Author Closing Comment

by:jindalee
ID: 31597202
Worked a treat..

Thank you!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Connection String 16 43
Error on link 14 37
I need help making a collection from a class in VB.Net 3 26
How useful is the free version of Selenium? 3 8
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

910 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now