Hello sir,
I want to search for nodes from the text given in textbox1.
After the search i want only those nodes to be displayed in treeview.

A treeview control shows items hierarchically. If you remove (there's no visible property in the node items) a node, have you thought what to do with the possible child nodes?

After you've figured that out, loop the nodes and remove non-matching nodes with TreeView1.Nodes.Remove method.

If you want some "find" functionality to your treeview you could also try something like

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  ' 
  TreeView1.Nodes.Add("foo1")
  TreeView1.Nodes.Add("bar")
  TreeView1.Nodes.Add("foo2")
  TreeView1.Nodes.Add("barfly")
  TreeView1.Nodes.Add("footer")
  
End Sub

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
  '
  Dim TextToSearch As String
  Static IndexToStartSearching As Integer = 0
  Dim i As Integer

  TextToSearch = "foo"
  For i = IndexToStartSearching To TreeView1.Nodes.Count - 1
    If TreeView1.Nodes.Item(i).Text.IndexOf(TextToSearch) >= 0 Then
      TreeView1.Nodes.Item(i).EnsureVisible()
      IndexToStartSearching = i + 1
      Exit For
    End If
  Next i

End Sub

but this shows only one matching item at the time.

A treeview control shows items hierarchically. If you remove (there's no visible property in the node items) a node, have you thought what to do with the possible child nodes?

After you've figured that out, loop the nodes and remove non-matching nodes with TreeView1.Nodes.Remove method.

If you want some "find" functionality to your treeview you could also try something like

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  ' 
  TreeView1.Nodes.Add("foo1")
  TreeView1.Nodes.Add("bar")
  TreeView1.Nodes.Add("foo2")
  TreeView1.Nodes.Add("barfly")
  TreeView1.Nodes.Add("footer")
  
End Sub

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
  '
  Dim TextToSearch As String
  Static IndexToStartSearching As Integer = 0
  Dim i As Integer

  TextToSearch = "foo"
  For i = IndexToStartSearching To TreeView1.Nodes.Count - 1
    If TreeView1.Nodes.Item(i).Text.IndexOf(TextToSearch) >= 0 Then
      TreeView1.Nodes.Item(i).EnsureVisible()
      IndexToStartSearching = i + 1
      Exit For
    End If
  Next i

End Sub

but this shows only one matching item at the time.

Hello sir,
That code is not displaying the nodes. When the click the button nothing happens.

EnsureVisible method should show and expand the tree to the selected node. It seems that it doesn't work as it is documented.

Here's a code that works with two levels

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  ' 
  Dim TempNode As TreeNode
  Dim ChildNode(0) As TreeNode
  TreeView1.Nodes.Add("foo1")
  ChildNode(0) = New TreeNode("foo2")
  TempNode = New TreeNode("bar", ChildNode)
  TreeView1.Nodes.Add(TempNode)
  TreeView1.Nodes.Add("barfly")
  TreeView1.Nodes.Add("footer")

End Sub

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
  '
  Dim TextToSearch As String
  Static IndexToStartSearching As Integer = 0
  Dim i As Integer
  Dim TempNode As TreeNode

  TextToSearch = "foo"
  For i = IndexToStartSearching To TreeView1.Nodes.Count - 1
    If TreeView1.Nodes.Item(i).Text.IndexOf(TextToSearch) >= 0 Then
      TreeView1.SelectedNode = TreeView1.Nodes.Item(i)
      TempNode = TreeView1.SelectedNode
      If TempNode.PrevNode IsNot Nothing Then
        TempNode.PrevNode.Expand()
      End If
      TempNode.Expand() ' Parent is expanded, this node is selected and now this node will be expanded
      TreeView1.Nodes.Item(i).EnsureVisible() ' This should show and expand to selected node
      IndexToStartSearching = i + 1
      Exit For
    End If
  Next i

End Sub

To make it work for all levels, you should make a procedure to locate path from the root level to the node you want to show and select and expand each node in the path starting from the root node.

I would call this a bug in the treeview control :@

You're using Windows Forms TreeView, right? Not ASP.NET TreeView? Code I gave works with a two level treeview. Like I said EnsureVisible should expand node (and parent nodes) but it does not seem to work like its documented.

This article has been dead for over six months. Start a new discussion instead.