1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

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

Discussion in 'C, C++, C#' started by MysteryGuest, Apr 4, 2013.

  1. MysteryGuest

    MysteryGuest Registered Member

    Joined:
    Mar 7, 2013
    Messages:
    63
    Likes Received:
    18
    Occupation:
    Freelancer
    Location:
    Virtual World!
    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: Apr 4, 2013