开发者

Search a list box while typing in a text box in Winforms C#

I have a list-box where items are loaded from database , when user types anything in text-box, list-box should search the starting characters entered and display it in text-box.?

For example :

As soon as User types "A" in text-box ,then all the records starting from "A" should be displayed in List-box.

When user selects th开发者_开发百科e record and press enter,that record should be entered in Text-box.

And also when user clicks the record from mouse,that record should be entered in Text-box.

I don't want to use text-box auto complete mode

How to do this in C# ?

Thanks in advance


A listbox itself does not support this kind of filtering behavior out of the box. You're best bet is to do the filtering yourself, each time the text changes in the textbox perform a search against the datasource and feed the results to the listbox.

As long as your datasource (the items in the database in this case) isn't gigantic you could get away with it by caching all the items and doing an in-memory search. At start up fetch all the items and from then on use the in-memory list for searches. Processing power isn't a problem these days, but this all depends on your current situation.

I don't know how large your datasource is & what your architecture/infrastructure is, but performing each "incremental" search against the database is probably not the fastest option. And speed is crucial with these kind of filtering mechanism.

If the in-memory list, nor going to the database each time, aren't possible/doable, Lucene.NET could be the most performing option. Lucene.NET has been made to create indexes of data and perform super fast searches against those indexes. The downside is that you'll have to create an index of your data and keep it up to date when your datasource changes, the upside is it's unbelievably fast. (no kidding :)

Link to Lucene.NET: http://incubator.apache.org/projects/lucene.net.html


int index = listbox.FindString(textbox.Text);
if (0 <= index)
{
    listbox.SelectedIndex = index;
}

EDIT: meh, answered based on the title question which, unfortunately, is quite self-contained while opposed to the actual question. Please ignore...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜