User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the VB.NET section within the Software Development category of DaniWeb, a massive community of 427,935 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,924 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our VB.NET advertiser: Programming Forums
Views: 495 | Replies: 9
Reply
Join Date: Apr 2008
Posts: 33
Reputation: KillerOfDN is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
KillerOfDN KillerOfDN is offline Offline
Light Poster

TreeViewCollection Help!

  #1  
Jul 13th, 2008
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
            treeNodes.Add(node)
        Next

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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2008
Location: Sivakasi, Tamilnadu, India
Posts: 460
Reputation: selvaganapathy is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 79
selvaganapathy's Avatar
selvaganapathy selvaganapathy is offline Offline
Posting Pro in Training

Re: TreeViewCollection Help!

  #2  
Jul 13th, 2008
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

Or
Are you want to copy all the nodes to your own collection or another treeview control?
Last edited by selvaganapathy : Jul 13th, 2008 at 10:53 pm.
KSG
Reply With Quote  
Join Date: Apr 2008
Posts: 33
Reputation: KillerOfDN is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
KillerOfDN KillerOfDN is offline Offline
Light Poster

Re: TreeViewCollection Help!

  #3  
Jul 14th, 2008
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
                    MsgBox("Found")

                    ' I need to add innerTreeNode to a collection?

                End If
            Next
            If treenode.Text.ToLower.Contains(txtSearchTerm.Text.ToLower) Then
                MsgBox("Found")

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

            End If
        Next

Thank you
Reply With Quote  
Join Date: Feb 2008
Location: Sivakasi, Tamilnadu, India
Posts: 460
Reputation: selvaganapathy is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 79
selvaganapathy's Avatar
selvaganapathy selvaganapathy is offline Offline
Posting Pro in Training

Re: TreeViewCollection Help!

  #4  
Jul 14th, 2008
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.
  1. 'Make It FormLevel
  2. Dim array As New ArrayList ()
  3. ....
  4.  
  5. If ..
  6. array.Add (treenode.Text)
  7. End If
KSG
Reply With Quote  
Join Date: Feb 2008
Location: Sivakasi, Tamilnadu, India
Posts: 460
Reputation: selvaganapathy is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 79
selvaganapathy's Avatar
selvaganapathy selvaganapathy is offline Offline
Posting Pro in Training

Re: TreeViewCollection Help!

  #5  
Jul 15th, 2008
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
  1. Dim array As New ArrayList()
  2. .....
  3. If Your Condition Then
  4. array.Add ( innerTreeNode )
  5. End If
  6. ....
  7. TreeView1.Nodes.Clear()
  8.  
  9. ForEach tNode As TreeNode In array
  10. ' Again Add to TreeView
  11. Next
KSG
Reply With Quote  
Join Date: Apr 2008
Posts: 33
Reputation: KillerOfDN is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
KillerOfDN KillerOfDN is offline Offline
Light Poster

Re: TreeViewCollection Help!

  #6  
Jul 15th, 2008
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?
Reply With Quote  
Join Date: Feb 2008
Location: Sivakasi, Tamilnadu, India
Posts: 460
Reputation: selvaganapathy is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 79
selvaganapathy's Avatar
selvaganapathy selvaganapathy is offline Offline
Posting Pro in Training

Re: TreeViewCollection Help!

  #7  
Jul 15th, 2008
Another approach instead of having collect the Nodes matches. Why dont you remove the Unmatch Nodes from the TreeView.
KSG
Reply With Quote  
Join Date: Apr 2008
Posts: 33
Reputation: KillerOfDN is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
KillerOfDN KillerOfDN is offline Offline
Light Poster

Re: TreeViewCollection Help!

  #8  
Jul 15th, 2008
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!
Reply With Quote  
Join Date: Feb 2008
Location: Sivakasi, Tamilnadu, India
Posts: 460
Reputation: selvaganapathy is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 79
selvaganapathy's Avatar
selvaganapathy selvaganapathy is offline Offline
Posting Pro in Training

Re: TreeViewCollection Help!

  #9  
Jul 20th, 2008
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?
KSG
Reply With Quote  
Join Date: Apr 2008
Posts: 33
Reputation: KillerOfDN is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
KillerOfDN KillerOfDN is offline Offline
Light Poster

Re: TreeViewCollection Help!

  #10  
Jul 20th, 2008
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.

Thanks
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb VB.NET Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the VB.NET Forum

All times are GMT -4. The time now is 6:26 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC