Fill ComboBox

2

In a User Registration Form, I have a UF combo box (which loads the data from a sql table), while typing in the combox the system needs to search the data and display despite the ones that match the typed one. I'm doing it this way:

    private void carregaComboEstadoCivil()
    {
        ProfissionalBLL profissional = new ProfissionalBLL();
        profissional.carregaComboEstadoCivil(cbxEstadoCivil);

        cbxEstadoCivil.KeyPress += new KeyPressEventHandler(cbxEstadoCivil_KeyPress);
    }

Professional Class BLL

    public void carregaComboEstadoCivil(ComboBox combo)
    {
        FuncionsDAL carrega = new FuncionsDAL();
        DataTable tabela = carrega.listaEstadoCivil();

        DataRow linha = tabela.NewRow();

        combo.DropDownStyle = ComboBoxStyle.DropDown;
        combo.AutoCompleteMode = AutoCompleteMode.Suggest;
        combo.DataSource = tabela;
        combo.DisplayMember = "Descricao";
        combo.ValueMember = "ID";
        combo.Update();
        linha["ID"] = 0;
        linha["Descricao"] = "Selecione...";
        tabela.Rows.InsertAt(linha, 0);
    }

KeyPress Event

    private void cbxEstadoCivil_KeyPress(object sender, KeyPressEventArgs e)
    {
        cbxEstadoCivil.DroppedDown = true;

    }

But it's not working.

The combobox opens allows me to write but not only shows the records that match what I wrote it shows me all the record.

    
asked by anonymous 03.09.2015 / 19:01

1 answer

1
    //criando palavras sugeridas
    var source = new AutoCompleteStringCollection();
    source.AddRange(new string[]
            {
                "Masculino",
                "Feminino",
                "Outro"
            });
    // criando combo
    var combobox = new ComboBox
    {
        AutoCompleteCustomSource = source,
        AutoCompleteMode =
            AutoCompleteMode.SuggestAppend,
        AutoCompleteSource =
            AutoCompleteSource.CustomSource,
        Location = new Point(20, 20),
        Width = ClientRectangle.Width - 40,
        Visible = true,
        DropDownStyle = ComboBoxStyle.DropDown
    };
    combobox.Items.Add("Masculino");
    combobox.Items.Add("Feminino");
    combobox.Items.Add("Outro");
    
03.09.2015 / 23:06