AniBankai
AniBankai

Reputation: 43

Filtering a combobox with a textbox

As the title suggests, I want to filter the values in a combobox according to what is written in a textbox. The combobox takes values from a list. I have tried AutoCompleteMode and AutoCompleteSource but it doesn't let me add any values to the combobox when I use these. The combobox holds values of a list of the following class.

class Groep
{
    //Fields
    private string naamGroep;


    //Properties
    public string NaamGroep
    {
        get { return this.naamGroep; }
        set { naamGroep = NaamGroep; }
    }



    //Constructor
    public Groep(string naam)
    {
        this.naamGroep = naam;

    }

This is the list:

List<Groep> Groepen = new List<Groep>();

I have two textboxes. One to add items to the list and the other to filter the combobox.

Upvotes: 0

Views: 858

Answers (1)

Ghasem
Ghasem

Reputation: 15573

Do it using a foreach loop

private void Button1_Click(object sender, EventArgs e)
{
    ComboBox1.Items.Clear();
    foreach (Groep g in Groepen.Where(g => g.NaamGroep.Contains(TextBox1.Text)))
        ComboBox1.Items.Add(g.NaamGroep);
}

Upvotes: 1

Related Questions