Hey guys/girls

I am having trouble adding Nodes from a TreeView into a TreeViewCollection
My code below:

Dim treeNodes As TreeNodeCollection
        For Each node As TreeNode In TreeView1.Nodes

I am getting Object Reference not set to an instance of an Object error... There is no Constructor available for the TreeNodeCollection type. What could be causing this?

Many thanks

8 Years
Discussion Span
Last Post by KillerOfDN

Hi, TreeNodeCollection is a Collection of Nodes and your object treeNodes does not refer any collection. so you getting that error
Do you want like that

Dim treeNodes As TreeNodeCollection
        treeNodes = TreeView1.Nodes

Are you want to copy all the nodes to your own collection or another treeview control?


Hi selvaganapathy,

The idea is, I search through every node (and sub nodes) in my TreeView and check if they match a search criteria, if the node matches the search, I need to add it to a collection. I then need to clear my TreeView and re-populate it with the collection of TreeNodes matching the Search. The code above was a simplified example of my problem, my actual program code is below:

Dim node As TreeNodeCollection =  TreeView1.Nodes(0).Nodes

        For Each treenode As TreeNode In node

            Dim innerNodes As TreeNodeCollection = treenode.Nodes
            For Each innerTreenode As TreeNode In innerNodes
                If innerTreenode.Text.ToLower.Contains(txtSearchTerm.Text.ToLower) Then

                    ' I need to add innerTreeNode to a collection?

                End If
            If treenode.Text.ToLower.Contains(txtSearchTerm.Text.ToLower) Then

                ' Again, I need to add treeNode to a collection?

            End If

Thank you


Here dont have reference to the Node because when you clear the TreeNodes it will no longer. So you separately have you own collection of data (data of Matching Node ) that you want show again.

You can use ArrayList () for Storing search data.

'Make It FormLevel
Dim array As New ArrayList ()

If ..
array.Add (treenode.Text)
End If

sorry, Nodes will exist even you clear the TreeNodes. so add the match node into array list and when finishing clear the TreeView then again add the nodes from the arraylist.

ArrayList can contain a collection of object.
Then your code may be

Dim array As New ArrayList()
    If  Your Condition  Then
         array.Add ( innerTreeNode ) 
    End If

    ForEach tNode As TreeNode In array
        ' Again Add to TreeView

The problem is, this code will only add the top level node to the search (the reason for using nodes in the first place). I need ALL the nodes that are inside the sub-nodes containing the search term! For example suppose I had 3 Level 0 nodes (Node1, Node2, Node3), now inside each of those nodes I had another 3 SubNodes (level 2). If the level 1 Node matches the search criteria I need to take it, and all of the nodes inside it (level 2 nodes) and add them back to the treeview?


Another approach instead of having collect the Nodes matches. Why dont you remove the Unmatch Nodes from the TreeView.


Yeah, I did try that too... However that also removes the subnodes from within the node that does'nt contain the search term... Thanks for your efforts though, greatly apreciated!


Hi, I tried your question with different approach, but end up with Errors. Sorry for the delayed post. The problem happens when the Child Node Contains Some Search item and Parent node does not contain the search item. Then what happer? Remove the Parent node or Keep it as it is.?

Do you find any Solution?


Hi selvaganapathy, thanks for your response once again.
I need to add the child node (if it contains the search term), and not the parent node (if it does'nt contain the search term). However, if the parent does contain the search term, I need that along with all of its child nodes.


This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.