How to filter items in a ListBox based on the search string
I have a Windows Forms (C #) Application containing a ListBox to which I have added some items (I am not using a DataSource). I want to filter the items in a ListBox to only show the items that contain the string that I am looking for.
I did this by saving a list of original items and selecting the corresponding items from that list every time the search bar changes and updates the ListBox.Items
Is there a more elegant / efficient way to do this?
a source to share
Is there a more elegant / efficient way?
No, not at all.
You can connect via BindingSource and have Filter and Sort properties, but that doesn't work for a simple List <>. So you have to use something like DataTable and it won't be an improvement.
Your current method seems fine, especially if you can use LINQ to filter the list.
But I hope you don't iterate over the Items property every time, just assign the filtered list to Listbox1.DataSource.
a source to share