wpcoder
wpcoder

Reputation: 1054

Filter a Treeview with a Textbox in a VB.NET winforms

Having the code bellow; something looks wrong in the logic, no error message is displayed. I am trying to filter a treeview1 control with a textbox1.text on change event.

The expected result is to keep in treeview1 the nodes that only similar to textbox1.text (%text% in SQL)

I found lots of results for solution in C# but can't figure it out in VB.NET.

Referencing this answer, and converting the code into VB.NET with little change still not working:

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        Try
            Dim FilterText As String = TextBox1.Text
            TreeView1.BeginUpdate()
            If (FilterText IsNot String.Empty) Then
                For Each Node As TreeNode In TreeView1.Nodes
                    If Not Node.Text.Contains(FilterText) Then
                        Node.Remove()
                        TreeView1.Refresh()
                    End If
                Next
            Else
                Exit Sub
            End If
            TreeView1.EndUpdate()
        Catch ex As Exception
        End Try
    End Sub

Upvotes: 0

Views: 3145

Answers (2)

wpcoder
wpcoder

Reputation: 1054

After several post's reading; It was very simple to apply filter to treeview. Even though I wanted initially to hide other nodes that is not in filter but this code is practical and do the job.

 Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        Dim TxtToFilter As String = TextBox1.Text
        Dim NodeItem As TreeNode
        For Each NodeItem In TreeView1.Nodes
            If NodeItem.Text.ToLower.Contains(TxtToFilter.ToLower)  And TxtToFilter IsNot String.Empty Then
                NodeItem.EnsureVisible()
                NodeItem.BackColor = Color.Yellow
            Else
                NodeItem.BackColor = TreeView1.BackColor
            End If
        Next
    End Sub

Note: This code works when all nodes are root/parents which is in my app logic. Didn't test for child nodes.

Upvotes: 0

Ramankingdom
Ramankingdom

Reputation: 1496

Here is the code I just color the selected node. you can change as per you need

Public Class Form1

Dim matchedNodes As List(Of TreeNode) = New List(Of TreeNode)
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged

    matchedNodes.Clear()
    If Not String.IsNullOrEmpty(TextBox1.Text) Then
        TreeView1.BeginUpdate()
        For Each node As TreeNode In TreeView1.Nodes
            FindRecursive(node)
        Next
        TreeView1.EndUpdate()

    End If
    For Each match In matchedNodes
        match.BackColor = Color.Yellow
    Next
    TreeView1.Refresh()
End Sub
Private Sub FindRecursive(treeNode As TreeNode)
    treeNode.BackColor = Color.White
    If treeNode.Text.Contains(TextBox1.Text) Then
        matchedNodes.Add(treeNode)
    End If

    For Each node As TreeNode In treeNode.Nodes
        If node.Text.Contains(TextBox1.Text) Then
            matchedNodes.Add(node)
        End If
        FindRecursive(node)
    Next
End Sub
End Class

Upvotes: 1

Related Questions