VB.Net Adding node and data to treeview

Hi Experts.
I am collecting strings of data from a database that must be stored in the appropriate nodes of a treeview.   The data is in the format ABC and XYZ, where ABC is the subject and XYZ is the data.     I need to search the nodes in the treeview to see if the new node, ABC already exists and add the node it if it does not.   Then I need to add the data XYZ to the correct node, either the new node that has just been created or an already existing node.  Would appreciate any code examples as I seem to be struggling with declarations of nodes from strings..  Many thanks.
LVL 2
PNRTAsked:
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.

CodeCruiserCommented:
You can use the

Treeview.Nodes.Item("Node Name")

to get the node. So something like this

Dim node As TreeNode = Treeview1.Nodes.Item("Node Name")
If Node is Nothing Then
   Treeview1.Nodes.Add("Node Name").Text = "Value"
Else
   Node.Text = "Value"
End If

https://msdn.microsoft.com/en-us/library/fk6c42hd(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/system.windows.forms.treenode(v=vs.110).aspx
0
PNRTAuthor Commented:
Thank you for your reply.  I had seen both of the links previously and it had not take me any further,
 
From your example, I cant see how this would check all the nodes in the treeview control and add the latest one if it was missing.

To try and solve the first part of my problem I tried this

For Each n As TreeNode In Me.TreeView.nodes
      If n.Text = ABC Then
             ''do nothing
      Else
                TreeView1.Nodes.Item(0).Nodes.Add(ABC)
      End If
Next

Which created the node ABC but failed because it entered it multiple times
0
CodeCruiserCommented:
That's because you are checking the root level but adding at lower level. Try changing

TreeView1.Nodes.Item(0).Nodes.Add(ABC)

to

TreeView1.Nodes.Add(ABC)
Exit For
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

PNRTAuthor Commented:
Hi.  This seems to be checking all the nodes multiple times and I end up with hundreds of duplicated notes
There should have been about 20 if not duplicated ........
Just to confirm, ABC is the string that I need to have a single node for

 For Each n As TreeNode In Me.TreeView1.Nodes  
            If n.Text = ABC Then

            Else
                TreeView1.Nodes.Add(ABC)
            End If
        Next
0
CodeCruiserCommented:
You missed the Exit For bit.
0
CodeCruiserCommented:
Actually, it needs to be like below

Dim Found As Boolean = False
 For Each n As TreeNode In Me.TreeView1.Nodes  
            If n.Text = ABC Then
               Found = True
               Exit For
            End If
        Next

If Not Found Then
      TreeView1.Nodes.Add(ABC)
End If
0
PNRTAuthor Commented:
Many thanks - that works fine
On the second part of my question, could you suggest how I could now add the data to the specific node?
Adding xyz to the specific node ABC?
0
CodeCruiserCommented:
You will use the existing code and where we are setting found to true, you would set node's value there
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
PNRTAuthor Commented:
Many Thanks.  
From my reading
   TreeView1.Nodes(ABC).Nodes.Add(XYZ)
Should add the string XYZ to the node ABC which we created

I've tried this

 Dim Found As Boolean = False
        For Each n As TreeNode In Me.TreeView1.Nodes
            If n.Text = ABC Then
                Found = True
                TreeView1.Nodes(ABC).Nodes.Add(XYZ)
                Exit For
            End If
        Next

        If Not Found Then
            TreeView1.Nodes.Add(ABC)
            TreeView1.Nodes(ABC).Nodes.Add(XYZ)
        End If

I added it to both found and not found as it always needs to be added whether the node is newly created or not.

This seems to add one node only the stop without an error

Is the addition of XYZ wrong?
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
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.