Click here to Skip to main content
Click here to Skip to main content

Auto Complete ComboBox

By , 8 Nov 2006
 

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)

About the Author

Manoh
Team Leader
India India
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 1memberMember 1003999612 May '13 - 22:32 
Questionmy votemembersaj_2111 Jun '12 - 4:19 
QuestionthnksmemberMember 850252911 Mar '12 - 4:47 
GeneralMy vote of 5memberT. Abdul Rahman8 Nov '11 - 21:54 
GeneralMy vote of 3membershanawazway27 Sep '10 - 21:51 
GeneralThanks a lot!memberTabitutza1 Jun '10 - 1:04 
QuestionDon't want to auto complete full word?memberimresoft29 Nov '09 - 5:00 
GeneralThanx a lotmemberMcGee8 Feb '09 - 17:58 
GeneralQuick Notememberchris17512 Feb '07 - 6:35 
GeneralFlaw...memberchris17512 Feb '07 - 6:24 
GeneraldetailmemberAlmustafa18 Nov '06 - 17:57 
GeneralDohmembernorm .net8 Nov '06 - 1:32 
GeneralRe: Dohmembersameeraperera8 Nov '06 - 6:28 

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

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