Click here to Skip to main content
11,431,483 members (64,245 online)
Click here to Skip to main content

Auto Complete ComboBox

, 8 Nov 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

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)

Share

About the Author

Manoh
Team Leader
India India
No Biography provided

Comments and Discussions

 
GeneralAutocomplete Imlementation with Jquery,PHP & Mysql Pin
camthach11-Jan-15 16:58
membercamthach11-Jan-15 16:58 
QuestionProblem with selectedvalue Pin
Avinash Sureka30-Aug-13 1:34
memberAvinash Sureka30-Aug-13 1:34 
GeneralMy vote of 1 Pin
Member 1003999612-May-13 23:32
memberMember 1003999612-May-13 23:32 
Questionmy vote Pin
saj_2111-Jun-12 5:19
membersaj_2111-Jun-12 5:19 
Questionthnks Pin
Member 850252911-Mar-12 5:47
memberMember 850252911-Mar-12 5:47 
GeneralMy vote of 5 Pin
T. Abdul Rahman8-Nov-11 22:54
memberT. Abdul Rahman8-Nov-11 22:54 
GeneralMy vote of 3 Pin
shanawazway27-Sep-10 22:51
membershanawazway27-Sep-10 22:51 
GeneralThanks a lot! Pin
Tabitutza1-Jun-10 2:04
memberTabitutza1-Jun-10 2:04 
QuestionDon't want to auto complete full word? Pin
imresoft29-Nov-09 6:00
memberimresoft29-Nov-09 6:00 
GeneralThanx a lot Pin
McGee8-Feb-09 18:58
memberMcGee8-Feb-09 18:58 
GeneralQuick Note Pin
chris17512-Feb-07 7:35
memberchris17512-Feb-07 7:35 
GeneralFlaw... Pin
chris17512-Feb-07 7:24
memberchris17512-Feb-07 7:24 
Generaldetail Pin
Almustafa18-Nov-06 18:57
memberAlmustafa18-Nov-06 18:57 
GeneralDoh Pin
norm .net8-Nov-06 2:32
membernorm .net8-Nov-06 2:32 
GeneralRe: Doh Pin
sameeraperera8-Nov-06 7:28
membersameeraperera8-Nov-06 7:28 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150428.2 | Last Updated 8 Nov 2006
Article Copyright 2006 by Manoh
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid