tags:

views:

170

answers:

2

I have a listbox & textbox above it. I want to the user to be able to search the listbox just by starting to type the letters of the word or part of the word in the textbox.

What is the best way to code this? The most efficient?

An example would be great.

+1  A: 

Have an originalList<> of items and a resultList<>

.NET 2.0 Iterate through each item in the listbox and

if(yourString.Contains(<the textbox text>)

Add to the searcedList and bind the list to the resultList<> That will give you all results in the list box where the text matches the textbox search.

Could then get fancy with some regex etc to match only whole words, just the start of words etc.

Edit - If the text length is changed to zero, or some min length then revert back to originalList<>

.NET 3.5 Use Linq to search through and pull out all results, could order this by the chars in the text box.

Just my 2p, sure there will be some much cleaner approaches posted.

Jammin
A: 

If the list is bound to List you can just do .Find with a suitable delegate.

kpollock