Click here to Skip to main content
Licence CPOL
First Posted 8 Nov 2006
Views 59,965
Downloads 3,071
Bookmarked 41 times

Auto Complete ComboBox

By | 8 Nov 2006 | Article
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)

About the Author

Manoh

Team Leader

India India

Member



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

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionthnks PinmemberMember 85025294:47 11 Mar '12  
GeneralMy vote of 5 PinmemberT. Abdul Rahman21:54 8 Nov '11  
GeneralMy vote of 3 Pinmembershanawazway21:51 27 Sep '10  
GeneralThanks a lot! PinmemberTabitutza1:04 1 Jun '10  
QuestionDon't want to auto complete full word? Pinmemberimresoft5:00 29 Nov '09  
GeneralThanx a lot PinmemberMcGee17:58 8 Feb '09  
GeneralQuick Note Pinmemberchris1756:35 12 Feb '07  
GeneralFlaw... Pinmemberchris1756:24 12 Feb '07  
Generaldetail PinmemberAlmustafa17:57 18 Nov '06  
GeneralDoh Pinmembernorm .net1:32 8 Nov '06  
GeneralRe: Doh Pinmembersameeraperera6:28 8 Nov '06  

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.

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