Click here to Skip to main content
Click here to Skip to main content
Go to top

Auto Complete ComboBox

, 8 Nov 2006
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

 
QuestionProblem with selectedvalue PinmemberAvinash Sureka30-Aug-13 0:34 
GeneralMy vote of 1 PinmemberMember 1003999612-May-13 22:32 
Questionmy vote Pinmembersaj_2111-Jun-12 4:19 
Questionthnks PinmemberMember 850252911-Mar-12 4:47 
GeneralMy vote of 5 PinmemberT. Abdul Rahman8-Nov-11 21:54 
GeneralMy vote of 3 Pinmembershanawazway27-Sep-10 21:51 
GeneralThanks a lot! PinmemberTabitutza1-Jun-10 1:04 
QuestionDon't want to auto complete full word? Pinmemberimresoft29-Nov-09 5:00 
GeneralThanx a lot PinmemberMcGee8-Feb-09 17:58 
GeneralQuick Note Pinmemberchris17512-Feb-07 6:35 
GeneralFlaw... Pinmemberchris17512-Feb-07 6:24 
Generaldetail PinmemberAlmustafa18-Nov-06 17:57 
GeneralDoh Pinmembernorm .net8-Nov-06 1:32 
GeneralRe: Doh Pinmembersameeraperera8-Nov-06 6: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 | Mobile
Web01 | 2.8.140916.1 | Last Updated 8 Nov 2006
Article Copyright 2006 by Manoh
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid