C # WinForms - ComboBox that autocompletes (filters) as you type

7

I have a ComboBox (cmbBairro) with the names of the neighborhoods of my city, which loads the values of a List (_ListBairros). When the user tries to search for a neighborhood by typing, I would like the ComboBox values to be filtered, regardless of the position of the typed characters!

When typing, for example: vis.

The ComboBox would filter:

Good Vis ta

Garden Vis ta Cheerful ...

How do I do this?

    
asked by anonymous 09.03.2015 / 02:20

1 answer

4

I do not quite understand if you wanted to filter in the ComboBox or in the ListBox, so I did the two, this filters directly on the ComboBox:

public partial class Form1 : Form
{
    public string[] bairros = new string[] {
            "Acari",
            "Anchieta",
            "Barros Filho",
            "Bento Ribeiro",
            "Brás de Pina",
            "Bonsucesso",
            "Campinho",
            "Cavalcanti",
            "Cascadura",
            "Coelho Neto",
            "Colégio",
            "Complexo do Alemão",
            "Cordovil",
            "Costa Barros",
            "Engenheiro Leal",
            "Engenho da Rainha",
            "Guadalupe",
            "Higienópolis",
            "Honório Gurgel",
            "Irajá",
            "Jardim América",
            "Madureira",
            "Marechal Hermes",
            "Manguinhos",
            "Oswaldo Cruz"
        };
    public Form1()
    {
        InitializeComponent();
        _ListBairros.Items.AddRange(bairros);
        cmbBairro.TextChanged += CmbBairro_TextChanged;
    }
    private void CmbBairro_TextChanged(object sender, EventArgs e) {
        string texto = cmbBairro.Text;
        int a = cmbBairro.Items.Count;          
        if (a > 0) {
           int i = 0;
           while (i < a) {
                cmbBairro.Items.RemoveAt(0);                      
                i++;
          }
       }
        if (texto != "") {
            System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(texto, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            foreach (string lugar in bairros) {
                System.Text.RegularExpressions.Match resultado = regex.Match(lugar);
                if (resultado.Value != "") {
                    cmbBairro.Items.Add(lugar);
                }
            }
        }         
    }
}

This filters in the ListBox:

public partial class Form1 : Form
{
    public string[] bairros = new string[] {
            "Acari",
            "Anchieta",
            "Barros Filho",
            "Bento Ribeiro",
            "Brás de Pina",
            "Bonsucesso",
            "Campinho",
            "Cavalcanti",
            "Cascadura",
            "Coelho Neto",
            "Colégio",
            "Complexo do Alemão",
            "Cordovil",
            "Costa Barros",
            "Engenheiro Leal",
            "Engenho da Rainha",
            "Guadalupe",
            "Higienópolis",
            "Honório Gurgel",
            "Irajá",
            "Jardim América",
            "Madureira",
            "Marechal Hermes",
            "Manguinhos",
            "Oswaldo Cruz"
        };
    public Form1()
    {
        InitializeComponent();
        _ListBairros.Items.AddRange(bairros);
        cmbBairro.TextChanged += CmbBairro_TextChanged;
    }

    private void CmbBairro_TextChanged(object sender, EventArgs e) {
        string texto = cmbBairro.Text;
        _ListBairros.Items.Clear();            
        if (texto != "") {
            System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(texto, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            foreach (string lugar in bairros) {
                System.Text.RegularExpressions.Match resultado = regex.Match(lugar);
                if (resultado.Value != "") {
                    _ListBairros.Items.Add(lugar);
                }
            }
        }         
    }
}

Within the string [] neighborhoods is where the neighborhoods are.

This is my first participation in StackOverflow, sorry for any error: P

    
26.09.2015 / 23:53