Fog Creek Software
Discussion Board

Welcome! and rules

Joel on Software

Autofill comboboxes in C#

Has anyone seen any C# solutions for an autofill combox?
(i.e. where you can type in text, and the combobox automatically matches it against entries in the drop-down list)

Any help much appreciated.

Paul Hutchinson
Friday, April 11, 2003

Presuming you're talking about Windows Forms, one exists in the Genghis project.

Brad (
Friday, April 11, 2003

I've seen it around in VB.NET:

Also checkout:

Hope it helps!

Friday, April 11, 2003

Try handling the keypress event for the combobox:
i.e. comboBox.KeyPress += new KeyPressEventHandler(comboBox_KeyPress); where comboBox is the instance of the comboBox and comboBox_KeyPress is defined as:

private void comboBox_KeyPress(object s, KeyPressEventArgs e)
    // If Enter is pressed, and a value has been entered
    if((e.KeyChar == (char)13) && (comboBox.Text != ""))
        // Add to the history if it hasn't been added already
        if(!comboBox.Items.Contains(comboBox.Text)) comboBox.Items.Add(comboBox.Text);
    // Else, if a letter, digit, punctuation, or separator has been pressed...
    else if((Char.IsLetterOrDigit(e.KeyChar)) || (Char.IsPunctuation(e.KeyChar))|| (Char.IsSeparator(e.KeyChar)))
        string text = comboBox.Text;
        if(text != "")
            // If value exists i.e. is in history
            if (comboBox.FindString(text)!=-1)
                comboBox.Text = (string)comboBox.Items[comboBox.FindString(text)];
            // now find portion of unmatched text and highlight as in browser address bars
            int diff_idx = comboBox.Text.Length - text.Length;
            if(diff_idx > 0)
                comboBox.Select(text.Length, diff);

Hope that helps. Tell me how well this works for you:-)

Friday, April 18, 2003

*  Recent Topics

*  Fog Creek Home