• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7703
  • Last Modified:

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

0
tonelm54
Asked:
tonelm54
  • 2
  • 2
1 Solution
 
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
 
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
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.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now