2
Contributors
5
Replies
8
Views
8 Years
Discussion Span
Last Post by Teme64
0

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.

0

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.

0

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 :@

0

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 topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.