Click here to Skip to main content
13,730,905 members
Click here to Skip to main content
Add your own
alternative version

Stats

129.6K views
11.2K downloads
48 bookmarked
Posted 8 Nov 2006
Licenced CPOL

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

You may also be interested in...

Comments and Discussions

 
Questionsmall typo Pin
Иван Иванов1-Sep-16 1:31
memberИван Иванов1-Sep-16 1:31 
GeneralAutocomplete Imlementation with Jquery,PHP & Mysql Pin
heightcelebs.com11-Jan-15 15:58
memberheightcelebs.com11-Jan-15 15:58 
QuestionProblem with selectedvalue Pin
Avinash Sureka30-Aug-13 0:34
memberAvinash Sureka30-Aug-13 0:34 
GeneralMy vote of 1 Pin
12-May-13 22:32
member12-May-13 22:32 
Questionmy vote Pin
saj_2111-Jun-12 4:19
membersaj_2111-Jun-12 4:19 
Questionthnks Pin
11-Mar-12 4:47
member11-Mar-12 4:47 
GeneralMy vote of 5 Pin
T. Abdul Rahman8-Nov-11 21:54
memberT. Abdul Rahman8-Nov-11 21:54 
GeneralMy vote of 3 Pin
shanawazway27-Sep-10 21:51
membershanawazway27-Sep-10 21:51 
GeneralThanks a lot! Pin
Tabitutza1-Jun-10 1:04
memberTabitutza1-Jun-10 1:04 
QuestionDon't want to auto complete full word? Pin
imresoft29-Nov-09 5:00
memberimresoft29-Nov-09 5:00 
GeneralThanx a lot Pin
McGee8-Feb-09 17:58
memberMcGee8-Feb-09 17:58 
GeneralQuick Note Pin
Christopher Stratmann12-Feb-07 6:35
memberChristopher Stratmann12-Feb-07 6:35 
GeneralFlaw... Pin
Christopher Stratmann12-Feb-07 6:24
memberChristopher Stratmann12-Feb-07 6:24 
Generaldetail Pin
Almustafa18-Nov-06 17:57
memberAlmustafa18-Nov-06 17:57 
GeneralDoh Pin
NormDroid8-Nov-06 1:32
memberNormDroid8-Nov-06 1:32 
GeneralRe: Doh Pin
sameeraperera8-Nov-06 6:28
membersameeraperera8-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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180920.1 | Last Updated 8 Nov 2006
Article Copyright 2006 by Manoh
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid