Search code examples
vb.netsql-likeflowlayoutpanel

search controls in flowlayoutpanel with "like" searchkey VB.NET


I have a flowlayoutpanel listing the titles of an album. Assuming the flowlayoutpanel has a lot of songs and I want to find a particular one. Let's say, I would like to get the song of Ed Sheeran "Thinking Out loud." So I would type the word "Thinking" in the searchbox, and without even finishing the "Out Loud" words, I would like to filter the flowlayoutpanel showing me the control that has the title "Thinking Out Loud" and hiding all the controls without the word "Thinking." It's like an SQL search LIKE query. But I don't want to do SQL. is it possible with FLOWLAYOUTPANEL and a SEARCH TEXTBOX?


Solution

  • You can use the search Textbox's TextChanged event to trigger a processing of the FlowLayoutPanel's controls collection setting each control's Visible property based on the result of a VB Like Operator comparison.

    Private Sub tbSearch_TextChanged(sender As Object, e As EventArgs) Handles tbSearch.TextChanged
        If tbSearch.Text.Length > 0 Then
            Dim compareTo As String = String.Concat("*", tbSearch.Text.ToLowerInvariant, "*")
            For Each c As Control In FlowLayoutPanel1.Controls
                c.Visible = (c.Text.ToLowerInvariant Like compareTo)
            Next
        Else
            For Each c As Control In FlowLayoutPanel1.Controls
                c.Visible = True
            Next
        End If
    End Sub