•
•
•
•
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
![]() |
•
•
Join Date: Apr 2008
Posts: 33
Reputation:
Rep Power: 1
Solved Threads: 1
Hey guys/girls
I am having trouble adding Nodes from a TreeView into a TreeViewCollection
My code below:
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
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)
NextI 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
•
•
Join Date: Feb 2008
Location: Sivakasi, Tamilnadu, India
Posts: 460
Reputation:
Rep Power: 1
Solved Threads: 79
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
Or
Are you want to copy all the nodes to your own collection or another treeview control?
Do you want like that
Dim treeNodes As TreeNodeCollection
treeNodes = TreeView1.NodesOr
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
•
•
Join Date: Apr 2008
Posts: 33
Reputation:
Rep Power: 1
Solved Threads: 1
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:
Thank you
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
•
•
Join Date: Feb 2008
Location: Sivakasi, Tamilnadu, India
Posts: 460
Reputation:
Rep Power: 1
Solved Threads: 79
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.
You can use ArrayList () for Storing search data.
VB.NET Syntax (Toggle Plain Text)
'Make It FormLevel Dim array As New ArrayList () .... If .. array.Add (treenode.Text) End If
KSG
•
•
Join Date: Feb 2008
Location: Sivakasi, Tamilnadu, India
Posts: 460
Reputation:
Rep Power: 1
Solved Threads: 79
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
ArrayList can contain a collection of object.
Then your code may be
vb.net Syntax (Toggle Plain Text)
Dim array As New ArrayList() ..... If Your Condition Then array.Add ( innerTreeNode ) End If .... TreeView1.Nodes.Clear() ForEach tNode As TreeNode In array ' Again Add to TreeView Next
KSG
•
•
Join Date: Apr 2008
Posts: 33
Reputation:
Rep Power: 1
Solved Threads: 1
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?
•
•
Join Date: Feb 2008
Location: Sivakasi, Tamilnadu, India
Posts: 460
Reputation:
Rep Power: 1
Solved Threads: 79
•
•
Join Date: Feb 2008
Location: Sivakasi, Tamilnadu, India
Posts: 460
Reputation:
Rep Power: 1
Solved Threads: 79
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?
Do you find any Solution?
KSG
![]() |
•
•
•
•
•
•
•
•
DaniWeb VB.NET Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Other Threads in the VB.NET Forum
- Previous Thread: VB.NET Label box + Timer
- Next Thread: dropdownlist


Linear Mode