vb.net listbox search

16,150

Solution 1

To make that work in that way, you will need a list in memory of all the items, and then ListBox1 would only show the matches. Otherwise, when the user hit's backspace to shorten their search phrase, none of the original items would come back. So, in the TextBox1_TextChanged event, the simplest way to do this would be to clear ListBox1, then loop though all the items in memory and then just add the ones that match to ListBox1. For instance:

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    ListBox1.Items.Clear()
    For Each item As String In allItems
        If item.StartsWith(TextBox1.Text, StringComparison.CurrentCultureIgnoreCase) Then
            ListBox1.Items.Add(item)
        End If
    Next
End Sub

In this example allItems is your in-memory list of all the items. If your items are strings, as it appears they are, then I would recommend just make it a List(Of String) and declare it at the class/form level as a private field:

private allItems As New List(Of String)()

Then, you would need to fill the list somewhere, probably in the form's Load event:

allItems.Add("cheese")
allItems.Add("eggs")
allItems.Add("milk")
allItems.Add("chicken")
allItems.Add("chocolate")

However, if all you need is an auto-complete text box, it's silly to re-invent the wheel. The WinForm TextBox control supports this functionality inherently with its AutoComplete properties.

Solution 2

    Dim lstBindTheseStrings As List(Of Object) = (From objString As Object _
                                                  In ListBox1.Items _
                                                  Where CStr(objString).StartsWith(TextBox1.Text)).ToList()

    ListBox1.DataSource = lstBindTheseStrings

    ListBox1.SelectedIndex = If((ListBox1.FindString(TextBox1.Text) > -1), _
                                 ListBox1.FindString(TextBox1.Text), -1)

Edit:

The above code will filter whats initially in the ListBox. SteveDog's solution is more of what you're looking for, but you can substitute ListBox1.Items in my Linq statement with your AllItems list to get to where you want to be.

Share:
16,150
Matt Levesque
Author by

Matt Levesque

Updated on June 04, 2022

Comments

  • Matt Levesque
    Matt Levesque almost 2 years

    I'm trying to make TextBox1 a search bar, to search for specific strings in ListBox1.

    I want it to remove other items that don't have the string I searched. For example, if a list contains (cheese, eggs, milk, chicken, chocolate) then searching for "ch" would only show cheese, chicken, and chocolate. Is this possible?

    This code I have here WILL search for the string, but doesn't eliminate others.

    EDIT: -These are all really great responses, but I can't use any of them because the listbox is being populated by filenames from a specific directory, which gives me this error;

    Items collection cannot be modified when the DataSource property is set.

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        Dim i As Integer = ListBox1.FindString(TextBox1.Text)
        ListBox1.SelectedIndex = i
        If TextBox1.Text = "" Then
            ListBox1.SelectedIndex = -1
        End If
    End Sub
    

    I appreciate any help. Thanks.

  • Matt Levesque
    Matt Levesque almost 12 years
    Thanks for the reply, but in this line of code, For Each item As String In allItems allItems is not declared?