C# Winforms - Autocomplete (filter) combobox when typing

Asked

Viewed 1,161 times

7

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

When typing, for example: vis.

The Combobox would filter:

Good Vista

Garden Vista Alegre ...

How do I do that?

  • See this implementation: http://www.codeproject.com/Tips/631196/ComboBox-with-Suggest-Ability-based-on-Substring-S @Marconi is a Windows application (Winforms) and not a web application. :)

  • Look at this other one: https://www.youtube.com/watch?v=HSBniHq5_zY

  • Opa blz, Give a look at this link it does what you’re looking for: (http://www.codeproject.com/Articles/16285/Auto-Complete-ComboBox) On this site you can download the code and put it on your system. Fix: I lowered it and just filter from the beginning of the text

  • 1

    @Hstackoverflow the code you quoted does exactly what I want... Just missed to give a HIGHLIGHT on the substring being typed :D Thanks

  • Not at all. @Leandrosjrp if possible post code as response to help other users with the same question. ;)

  • Sure, as soon as I finish the implementation I will post here! Hug!

  • Have you solved your problem yet? In case you haven’t solved yet, just change the Autocompletemode property from Combobox to Suggestappend, and that’s it. You don’t even need code. If served mark as answer. T+

  • Standard Autocomplete only completes at the beginning of the string not anywhere.

  • You can use the combobox of jqueryui which does exactly what you need. https://jqueryui.com/autocomplete/#combobox

Show 4 more comments

1 answer

4


I didn’t understand very well if you wanted to filter in Combobox or Listbox, so I did both, this filter direct in 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 filter in 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);
                }
            }
        }         
    }
}

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

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

  • 1

    Very good @Roberto Araujo... lacked a detail: In the event Textchanged has an if: if(text !="" ).... i added an Else: cmbBairro.Items.Addrange(neighborhoods);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.