VB.net TreeView find node

Im playing with the treeview control, but cant find if a particular path exists,

What Im trying to do is test if a particular path exists from root and return yes or no.

To try and test my idea, ive written the code below which should give the msgbox "Found path!"

Any ideas?
TreeView1.Nodes.Add("Nodes_1").Nodes.Add("Nodes_1.1").Nodes.Add("Nodes_1.1.1")
        If TreeView1.Nodes.Find("Nodes_1\Nodes1.1\Nodes_1.1.2", False) Is Nothing Then
            MsgBox("Not Found!")
        Else
            MsgBox("Found path!")
        End If

Open in new window

tonelm54Asked:
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.

Juan_BarreraCommented:
Hi again!

Look, this works for me, have a look:

 Dim tv As New TreeView
        Dim root As New TreeNode("Personal")
        tv.Nodes.Add(root)
        Dim child1 As New TreeNode("Banking")
        root.ChildNodes.Add(child1)
        Dim child2 As New TreeNode("Insurance")
        root.ChildNodes.Add(child2)
        Dim child3 As New TreeNode("Alliance and Leicester")
        child1.ChildNodes.Add(child3)
        Dim fn As TreeNode = tv.FindNode("Personal/Banking/Alliance and Leicester")
        If fn IsNot Nothing Then
            MsgBox("Found path!")
        Else
            MsgBox("Not Found!")
        End If

Open in new window

0
tonelm54Author Commented:
lol, Hi I opened a new question to go along another idea path.

When I try and run your code I get:-
          'ChildNodes' is not a member of 'System.Windows.Forms.TreeNode'.
0
Jorge PaulinoIT Pro/DeveloperCommented:
Juan_Barrera,
That's for ASP.NET not winforms, right ?
 
tonelm54,
Check this example (snippet).
Nodes.Find finds by the key and not on the text. You can include the path on the key and leave the text as you need. Then you can find by full path.

' Adds the node
Dim node As TreeNode = TreeView1.Nodes.Add("Nodes_1", "Nodes_1")
node.Nodes.Add("Nodes_1\Nodes_1.1", "Nodes_1.1").Nodes.Add("Nodes_1\Nodes_1.1\Nodes_1.1.1", "Nodes_1.1.1")
 
' Finds by full path
Dim node() As TreeNode = TreeView1.Nodes.Find("Nodes_1\Nodes_1.1\Nodes_1.1.1", True)
If node.Length > 0 Then
     MessageBox.Show("Found!")
End If

Open in new window

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
Juan_BarreraCommented:
@jpaulino,

Yes! I've just noticed that and was about to upload a snippet for WinForms. Points for you then! :)
0
tonelm54Author Commented:
Kool, that works!

Thank you
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.