2

I have implemented a simple function in VB .NET that searches the name of a node inside a TreeView and if found then highlights that specific node. After that I want to expand only the path of the TreeView that the node belongs to.

Is it possible to implement that with a VB function by giving the path of the TreeView or do I have to implement a separate function for that?

Below you can see my code

Private Sub FindRecursive(ByVal tNode As TreeNode)

    Dim tn As TreeNode

    For Each tn In tNode.Nodes
        If tn.Text = itemCode.Text Then
            tn.BackColor = Color.Yellow
            path1 = tNode.Tag
            Dim filename As String = System.IO.Path.Combine(path1, tn.Text)
            PictureBox3.Image = Image.FromFile(filename)

            'tNode.Expand()
            'TreeView1.TopNode.Expand()
            'TreeView1.ExpandAll()
            'tNode.Expand()
            'tNode.ExpandAll()
        End If

        FindRecursive(tn)
    Next

End Sub
Csalt
  • 45
  • 11
  • not really. There is a generic `.Controls.Find` but it might be too slow as it searches all child controls https://stackoverflow.com/questions/3898588/find-control-by-name-from-windows-forms-controls – Slai May 28 '17 at 16:13
  • @Slai I dont want the function that searcher the TreeView but a function that expand only a specific path of the selected node in the TreeView – Csalt May 28 '17 at 16:57

1 Answers1

0

I think you want to call the TreeNode.EnsureVisible method.

https://msdn.microsoft.com/en-us/library/system.windows.forms.treenode.ensurevisible(v=vs.110).aspx

You might be interested in looking up TreeNodeCollection.Find(key:=itemCode.Text, searchAllChildren:=True); it might implement the same functionality in finding a node that you have written yourself. Here is the MSDN Link: https://msdn.microsoft.com/en-us/library/system.windows.forms.treenodecollection.find(v=vs.110).aspx

Gorexxar
  • 1
  • 1