Click here to Skip to main content
15,868,419 members
Articles / Desktop Programming / Windows Forms

Auto Complete ComboBox

Rate me:
Please Sign up or sign in to vote.
4.50/5 (26 votes)
8 Nov 2006CPOL 161.8K   13K   48   16
An article on an auto-complete combobox.

Sample Image - AutoCompleteComboBox.gif

Introduction

This is a simple code snippet which is used to make an Auto Complete ComboBox.

Using the code

Usage: Call the function AutoComplete from within the ComboBox's KeyPress event handler.

VB
AutoComplete(ComboBox cb, System.Windows.Forms.KeyPressEventArgs e, bool blnLimitToList)

// AutoComplete
public void AutoComplete(ComboBox cb, System.Windows.Forms.KeyPressEventArgs e)
{
    this.AutoComplete(cb, e, false);
}

public void AutoComplete(ComboBox cb, 
       System.Windows.Forms.KeyPressEventArgs e, bool blnLimitToList)
{
    string strFindStr = "";

    if (e.KeyChar == (char)8) 
    {
        if (cb.SelectionStart <= 1) 
        {
            cb.Text = "";
            return;
        }

        if (cb.SelectionLength == 0)
            strFindStr = cb.Text.Substring(0, cb.Text.Length - 1);
        else 
            strFindStr = cb.Text.Substring(0, cb.SelectionStart - 1);
    }
    else 
    {
        if (cb.SelectionLength == 0)
            strFindStr = cb.Text + e.KeyChar;
        else
            strFindStr = cb.Text.Substring(0, cb.SelectionStart) + e.KeyChar;
    }

    int intIdx = -1;

    // Search the string in the ComboBox list.

    intIdx = cb.FindString(strFindStr);

    if (intIdx != -1)
    {
        cb.SelectedText = "";
        cb.SelectedIndex = intIdx;
        cb.SelectionStart = strFindStr.Length;
        cb.SelectionLength = cb.Text.Length;
        e.Handled = true;
    }
    else
    {
        e.Handled = blnLimitToList;
    }
}

History

  • Released on November 8, 2006.

License

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


Written By
Team Leader
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionsmall typo Pin
Иван Иванов1-Sep-16 1:31
Иван Иванов1-Sep-16 1:31 
GeneralAutocomplete Imlementation with Jquery,PHP & Mysql Pin
heightcelebs.com11-Jan-15 15:58
heightcelebs.com11-Jan-15 15:58 
QuestionProblem with selectedvalue Pin
Avinash Sureka30-Aug-13 0:34
Avinash Sureka30-Aug-13 0:34 
GeneralMy vote of 1 Pin
Member 1003999612-May-13 22:32
Member 1003999612-May-13 22:32 
Questionmy vote Pin
saj_2111-Jun-12 4:19
saj_2111-Jun-12 4:19 
Questionthnks Pin
Member 850252911-Mar-12 4:47
Member 850252911-Mar-12 4:47 
GeneralMy vote of 5 Pin
T. Abdul Rahman8-Nov-11 21:54
T. Abdul Rahman8-Nov-11 21:54 
GeneralMy vote of 3 Pin
shanawazway27-Sep-10 21:51
shanawazway27-Sep-10 21:51 
Good
GeneralThanks a lot! Pin
Tabitutza1-Jun-10 1:04
Tabitutza1-Jun-10 1:04 
QuestionDon't want to auto complete full word? Pin
imresoft29-Nov-09 5:00
imresoft29-Nov-09 5:00 
GeneralThanx a lot Pin
McGee8-Feb-09 17:58
McGee8-Feb-09 17:58 
GeneralQuick Note Pin
Christopher Stratmann12-Feb-07 6:35
Christopher Stratmann12-Feb-07 6:35 
GeneralFlaw... Pin
Christopher Stratmann12-Feb-07 6:24
Christopher Stratmann12-Feb-07 6:24 
Generaldetail Pin
Almustafa18-Nov-06 17:57
Almustafa18-Nov-06 17:57 
GeneralDoh Pin
NormDroid8-Nov-06 1:32
professionalNormDroid8-Nov-06 1:32 
GeneralRe: Doh Pin
sameeraperera8-Nov-06 6:28
sameeraperera8-Nov-06 6:28 

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.