0
votes

How to search ListBox using the same criteria as like "%%" in sql for example if the list contains the following items {cat , dog , cat with ring,dog with bone} and entered "with" in the textbox. i need to filter this listbox to only have records containig the word "with" ( i.e {cat with ring,dog with bone}).

so far i can search and select the item starting with the input string using this code ..

    private void txtSearch_TextChanged(object sender, EventArgs e)
    {
        int index = lst.FindString(this.txtSearch.Text);
        if (0 <= index)
        {
            lst.SelectedIndex = index;
        }
    }
1
how is your list box being populated? is it bound to a data source? Have you considered using string.Contains to filter it?Michael Edenfield

1 Answers

2
votes

Something like this should do it:

string searchTerm = this.txtSearch.Text;
var items = lst.Items.Cast<ListItem>().Where(t=>t.Value.Contains(searchTerm));

items will then contain all ListItems that have a Value that contains your search term.