C# Search [Filtered search(Any ListBox)] - EXAMPLE

MysteryGuest

Registered Member
Joined
Mar 7, 2013
Messages
64
Reaction score
18
Well i needed this for my Spinner Application to filter out synonyms and articles. So why not share it.

So lets add a text-box on our form. and ofc a List-box, this also can be a check list-box. Lets start with declaring a list.

Code:
///
using System.Collections.Generic;

//Our list
private List<string> _list;
once this is done we gonna make a function to save this list! clbArticles is my list-box.

Code:
//call this function after having the data in our list-box, for example; i implemented this once the query is done executing it automatically execute this function.
private void SaveArticleList()
        {
            _list = clbArticles.Items.Cast<string>().ToList();
        }

Now for searching, double click you textbox. so you get the even _TextChanged.
Code:
private void txtArticleSearch_TextChanged(object sender, EventArgs e)
        {
            if (_list.Count <= 0) return;
            clbArticles.Items.Clear();
            clbArticles.Items.AddRange(_list.Where(i => i.Contains(txtArticleSearch.Text)).ToArray());
        }

Well that's basically it! The reason why i save the data in our created list is simple; It's Faster!

Have fun coding!
 
Last edited:
Back
Top