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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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
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
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
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

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
CodeCruiserCommented:
You missed the Exit For bit.
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
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?
CodeCruiserCommented:
You will use the existing code and where we are setting found to true, you would set node's value there

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?
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.