Click here to Skip to main content
13,003,150 members (58,544 online)
Click here to Skip to main content
Add your own
alternative version


12 bookmarked
Posted 25 Jun 2014

Create combobox with search and suggest list

, 25 Jun 2014
Rate this:
Please Sign up or sign in to vote.
How to make a combobox with search and suggest list


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

Download ComboboxWithSearching.rar


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):

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
    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.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:

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 :).


Version 1.1


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


About the Author

Vinh Nguyễn Toàn
Software Developer (Junior)
Vietnam Vietnam
No Biography provided

You may also be interested in...

Comments and Discussions

Questiondoes it have library for mfc? Pin
yixuan1781-Nov-14 0:00
memberyixuan1781-Nov-14 0:00 
QuestionGood! Pin
ToBecomeDay25-Jun-14 16:18
memberToBecomeDay25-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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170626.1 | Last Updated 25 Jun 2014
Article Copyright 2014 by Vinh Nguyễn Toàn
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid