Click here to Skip to main content
15,886,024 members
Articles / Programming Languages / C#
Tip/Trick

Create combobox with search and suggest list

Rate me:
Please Sign up or sign in to vote.
4.67/5 (10 votes)
25 Jun 2014CPOL1 min read 53.2K   9   11   7
How to make a combobox with search and suggest list

Introduction

This article guide to to create a combobox that can search and has suggest list.

Download ComboboxWithSearching.rar

Background

More often than not, when there are so many items in the combobox, adding search feature and a suggest list maybe a good enhancement.

The most basic way to do search in combobox is utilize Autocomplete feature, but it exhibits only string.StartWith() search and there is no way to override that, so to make the combobox search in anywhere you like, with suggest list like Autocomplete do, we need another way out.

Using the code

We will use a listbox to simulate the suggest list of Autocomplete. Firstly, we add a listbox right below the combobox (using the Location property in Property panel maybe easier to do that):

Then we want the combobox do the search everytime we type something in, so we handle the TextChanged event of the combobox (TextUpdated is fine, too):

C#
private void comboBox1_TextChanged(object sender, EventArgs e)
{
    // get the keyword to search
    string textToSearch = comboBox1.Text.ToLower();
    listBox1.Visible = false; // hide the listbox, see below for why doing that
    if (String.IsNullOrEmpty(textToSearch))
        return; // return with listbox hidden if the keyword is empty
    //search
    string[] result = (from i in collections
            where i.ToLower().Contains(textToSearch)
            select i).ToArray();
    if (result.Length == 0)
        return; // return with listbox hidden if nothing found

    listBox1.Items.Clear(); // remember to Clear before Add
    listBox1.Items.AddRange(result);
    listBox1.Visible = true; // show the listbox again
}

After that, when we type in the combobox, the listbox shows with the search results (note that the search is case insensitive):

Then we want when user click on an item in the suggest list, the list disappears and the item is displayed on the combobox; we will handle listbox's SelectedIndexChanged event:

C#
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    comboBox1.SelectedItem = listBox1.SelectedItem;
    listBox1.Visible = false;
}

Here the combobox displays the same item type as the listbox so the code is short (you should have handled combobox's SelectedIndexChanged event).

There you go, a combobox with searching and suggest list as you type!

Points of Interest

The main idea of using the listbox as the suggest list can be expanded in a broader context, such as textbox with custom autocomplete or a search box that suggests items by images :).

History

Version 1.1

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Junior)
Vietnam Vietnam
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionCollections Pin
Member 1497755028-Oct-20 3:04
Member 1497755028-Oct-20 3:04 
QuestionHow to do this with the numbers? Pin
Member 132123338-Sep-18 4:20
Member 132123338-Sep-18 4:20 
Questionhow to do the same for value from database Pin
Member 118712965-Dec-17 21:50
Member 118712965-Dec-17 21:50 
Questioncollection does not exist error Pin
Member 118712965-Nov-17 18:22
Member 118712965-Nov-17 18:22 
AnswerRe: collection does not exist error Pin
Member 1589082410-Jan-23 22:29
Member 1589082410-Jan-23 22:29 
Questiondoes it have library for mfc? Pin
yixuan1781-Nov-14 0:00
yixuan1781-Nov-14 0:00 
QuestionGood! Pin
ToBecomeDay25-Jun-14 16:18
ToBecomeDay25-Jun-14 16:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.