שתף באמצעות


How to use Treenode.Find method?

Question

Tuesday, June 24, 2014 12:57 PM

Hi everyone,

I have been trying to use the treenode find method but dont seem to be able to get it. Here is a function I made based on the msdn sample but the return value = 0? Should return the node but returnValue seems to be something besides a normal treenode? ie What am I missing?

I see discussion about the ValuePath but I cant make sense out of it. Is that what the function returns? An array of chr?

I just want a simple way to get the treenode when I know the treenode.text. I have a recursive function that works but it seems there might be something simpler, like this find?

Public Class Form3
    Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        With TreeView1
            .HideSelection = False

            Dim NewNode As TreeNode
            Dim ChildNode As TreeNode
            Dim GrandChildNode As TreeNode

            .Nodes.Add("Node 1")

            NewNode = .Nodes.Add("Node 2")
            ChildNode = NewNode.Nodes.Add("Child 1")
            GrandChildNode = ChildNode.Nodes.Add("GrandChild 1")

            ChildNode = NewNode.Nodes.Add("Child 2")

            .Nodes.Add("Node 3")

        End With
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        Dim instance As TreeNodeCollection = TreeView1.Nodes
        Dim key As String = TreeView1.SelectedNode.Text
        Dim searchAllChildren As Boolean = True
        Dim returnValue As TreeNode()

        returnValue = instance.Find(key, searchAllChildren)

        MsgBox(returnValue.ToString)

    End Sub
End Class

All replies (10)

Tuesday, June 24, 2014 2:49 PM ✅Answered | 1 vote

Tommy,

From that MSDN document:

Parameters

key
The name of the tree node to search for.

searchAllChildren
true to search child nodes of tree nodes; otherwise, false.

Return Value

An array of TreeNode objects whose Name property matches the specified key.

The return value is an array of TreeNodes and the key - what to search for - is the .Name of the TreeNode, not the .Text property of it.

An example:

Option Strict On
Option Explicit On

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles MyBase.Load

        Dim tn As New TreeNode("Level 1")
        tn.Name = tn.Text

        Dim subTN As New TreeNode("Level 2")
        subTN.Name = subTN.Text

        Dim subSubTN As New TreeNode("Level 3")
        subSubTN.Name = subSubTN.Text

        subTN.Nodes.Add(subSubTN)
        tn.Nodes.Add(subTN)

        TreeView1.Nodes.Add(tn)

        Dim nodes() As TreeNode = TreeView1.Nodes.Find("Level 3", True)

        Stop

    End Sub

End Class

Tuesday, June 24, 2014 3:38 PM ✅Answered | 1 vote

 Well, i tried it by adding the nodes in the Load event and it works the same.  :)

Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim parentnode As New TreeNode With {.Name = "Node1", .Text = "Parent Node", .ImageIndex = 0}
        Dim childnode1 As New TreeNode With {.Name = "Node2", .Text = "Child Node 1", .ImageIndex = -1}
        Dim childnode2 As New TreeNode With {.Name = "Node3", .Text = "Child Node 2", .ImageIndex = -1}
        Dim grandchildnode As New TreeNode With {.Name = "Node4", .Text = "Grandchild Node", .ImageIndex = -1}

        childnode1.Nodes.Add(grandchildnode)
        parentnode.Nodes.Add(childnode1)
        parentnode.Nodes.Add(childnode2)
        TreeView1.Nodes.Add(parentnode)
    End Sub

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        Dim key As String = TreeView1.SelectedNode.Name
        Dim searchAllChildren As Boolean = True
        Dim returnValue As TreeNode()

        returnValue = TreeView1.Nodes.Find(key, searchAllChildren)

        MsgBox(returnValue(0).Text)
    End Sub
End Class

If you say it can`t be done then i`ll try it


Tuesday, June 24, 2014 1:03 PM

PS If I try to get the node text by returnValue.text vb says .text is not a valid property of returnValue, but I thought returnValue is the treenode?

PSS When I try:

   MsgBox(returnValue(0).ToString)

vb says its outside bounds of array.


Tuesday, June 24, 2014 3:17 PM

Tommy,

From that MSDN document:

Parameters

key
The name of the tree node to search for.

searchAllChildren
true to search child nodes of tree nodes; otherwise, false.

Return Value

An array of TreeNode objects whose Name property matches the specified key.

The return value is an array of TreeNodes and the key - what to search for - is the .Name of the TreeNode, not the .Text property of it.

An example:

Option Strict On
Option Explicit On

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles MyBase.Load

        Dim tn As New TreeNode("Level 1")
        tn.Name = tn.Text

        Dim subTN As New TreeNode("Level 2")
        subTN.Name = subTN.Text

        Dim subSubTN As New TreeNode("Level 3")
        subSubTN.Name = subSubTN.Text

        subTN.Nodes.Add(subSubTN)
        tn.Nodes.Add(subTN)

        TreeView1.Nodes.Add(tn)

        Dim nodes() As TreeNode = TreeView1.Nodes.Find("Level 3", True)

        Stop

    End Sub

End Class

I see. Thanks.

Hmmm, it does not return an exact child it returns all the children in the branch. I guess I can iterate that now. I can use it to improve the recursive function that I made.

Unless you can tell .find to return the exact child node? For example when I have child node 1 selected I get the whole branch (see image).

This treeview control sure is difficult to use. Everything I go to do it seems I have to fight some dumb little thing. Better than making my own of course.

Here is my example with corrected code for anyone interested.

Public Class Form3
    Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        With TreeView1
            .HideSelection = False

            Dim NewNode As TreeNode
            Dim ChildNode As TreeNode
            Dim GrandChildNode As TreeNode

            NewNode = .Nodes.Add("Node 1")
            NewNode.Name = NewNode.Text

            NewNode = .Nodes.Add("Node 2")
            NewNode.Name = NewNode.Text

            ChildNode = NewNode.Nodes.Add("Child 1")
            ChildNode.Name = NewNode.Text

            GrandChildNode = ChildNode.Nodes.Add("GrandChild 1")
            GrandChildNode.Name = NewNode.Text

            ChildNode = NewNode.Nodes.Add("Child 2")
            ChildNode.Name = NewNode.Text

            NewNode = .Nodes.Add("Node 3")
            NewNode.Name = NewNode.Text

        End With
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim instance As TreeNodeCollection = TreeView1.Nodes
        Dim key As String = TreeView1.SelectedNode.Name
        Dim searchAllChildren As Boolean = True
        Dim returnValue As TreeNode()

        returnValue = instance.Find(key, searchAllChildren)

        MsgBox(returnValue(0).ToString)

    End Sub
End Class


Tuesday, June 24, 2014 3:20 PM

Tommy,

Honestly I've never worked with it.

I knew it existed, but if you remember, I built my own class to "create nodes" and with that, I just use LINQ to find whatever I need to find.

Still lost in code, just at a little higher level.

:-)


Tuesday, June 24, 2014 3:28 PM

Hi Tom,

 I just tried your code with a slight change but, i added the nodes in the treeviews properties on the Design window and it works on my end. Maybe adding them in code makes it different.

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        Dim key As String = TreeView1.SelectedNode.Name
        Dim searchAllChildren As Boolean = True
        Dim returnValue As TreeNode()

        returnValue = TreeView1.Nodes.Find(key, searchAllChildren)

        MsgBox(returnValue(0).Text)
    End Sub

If you say it can`t be done then i`ll try it


Tuesday, June 24, 2014 4:32 PM

 Well, i tried it by adding the nodes in the Load event and it works the same.  :)

Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim parentnode As New TreeNode With {.Name = "Node1", .Text = "Parent Node", .ImageIndex = 0}
        Dim childnode1 As New TreeNode With {.Name = "Node2", .Text = "Child Node 1", .ImageIndex = -1}
        Dim childnode2 As New TreeNode With {.Name = "Node3", .Text = "Child Node 2", .ImageIndex = -1}
        Dim grandchildnode As New TreeNode With {.Name = "Node4", .Text = "Grandchild Node", .ImageIndex = -1}

        childnode1.Nodes.Add(grandchildnode)
        parentnode.Nodes.Add(childnode1)
        parentnode.Nodes.Add(childnode2)
        TreeView1.Nodes.Add(parentnode)
    End Sub

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        Dim key As String = TreeView1.SelectedNode.Name
        Dim searchAllChildren As Boolean = True
        Dim returnValue As TreeNode()

        returnValue = TreeView1.Nodes.Find(key, searchAllChildren)

        MsgBox(returnValue(0).Text)
    End Sub
End Class

If you say it can`t be done then i`ll try it

Isnt that weird? I get the exact child node using your example but with my example I get the whole branch, as I showed above. I can't spot the difference between the two.


Tuesday, June 24, 2014 5:16 PM | 1 vote

Hey,

 It is because, the nodes your adding to the treeview don`t have a name. That is why i created mine as New nodes and assigned them a name. As Frank mentioned, the Find method needs the name of the node to find it.

 You can test this by adding a listbox and another button to the form and looping threw the treeview nodes and try listing the names of the nodes. With your code it does not add anything to the listbox for the names because they are not assigned a name but, then try it by creating New nodes like i did (adding names) and it will list the node names in the listbox.   :)

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'this only lists the parent nodes names
        For Each n As TreeNode In TreeView1.Nodes
            ListBox1.Items.Add(n.Name)
        Next
    End Sub

EDIT : I added that it will only list the parent node names

If you say it can`t be done then i`ll try it


Tuesday, June 24, 2014 5:38 PM

Hey,

 It is because, the nodes your adding to the treeview don`t have a name. That is why i created mine as New nodes and assigned them a name. As Frank mentioned, the Find method needs the name of the node to find it.

 You can test this by adding a listbox and another button to the form and looping threw the treeview nodes and try listing the names of the nodes. With your code it does not add anything to the listbox for the names because they are not assigned a name but, then try it by creating New nodes like i did (adding names) and it will list the node names in the listbox.   :)

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'this only lists the parent nodes names
        For Each n As TreeNode In TreeView1.Nodes
            ListBox1.Items.Add(n.Name)
        Next
    End Sub

EDIT : I added that it will only list the parent node names

If you say it can`t be done then i`ll try it

Oh I see. Well that is what you get when you go round and round enough for a few days. I did name some of the nodes incorrectly. Now its working to return the exact child.

However, now I want to use it to return the whole branch, like it was. Thats handy too. :)

        With TreeView1
            '.HideSelection = False

            Dim NewNode As TreeNode
            Dim ChildNode As TreeNode
            Dim GrandChildNode As TreeNode

            NewNode = .Nodes.Add("Node 1")
            NewNode.Name = NewNode.Text

            NewNode = .Nodes.Add("Node 2")
            NewNode.Name = NewNode.Text

            ChildNode = NewNode.Nodes.Add("Child 1")
            ChildNode.Name = ChildNode.Text

            GrandChildNode = ChildNode.Nodes.Add("GrandChild 1")
            GrandChildNode.Name = GrandChildNode.Text

            ChildNode = NewNode.Nodes.Add("Child 2")
            ChildNode.Name = ChildNode.Text

            NewNode = .Nodes.Add("Node 3")
            NewNode.Name = NewNode.Text

        End With

Tuesday, June 24, 2014 9:31 PM

Tommy,

Sorry it seems like I ignored you here - I was in meetings.

Anyway, I'm glad you got it sorted out. :)

Still lost in code, just at a little higher level.

:-)