1

I want my combobox to enable the autocomplete if what the user is typing is in the items list and if it doesn't exists, I want to include it in my list.

For example:

A ComboBox with these items: "Rock, Country, Jazz". If the user starts typing "Ro..." the combobox autocomplete to 'Rock'. But if the user types "Blues", I want to add it to my items. So it would be like: "Rock, Country, Jazz, Blues".

How can I do that?

Evil Str
  • 132
  • 9

2 Answers2

3

You can use AutoCompleteMode and AutoCompleteSource for auto complete.

comboBox1.AutoCompleteMode = AutoCompleteMode.Append;
comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;

or you can do this via Properties Panel in Visual Studio after selecting your ComboBox...

For adding new items to your ComboBox;

  private void comboBox1_TextChanged(object sender, EventArgs e)
    {
        if (!comboBox1.Items.Contains(comboBox1.Text))
        {
            comboBox1.Items.Add(comboBox1.Text);
            comboBox1.Items.RemoveAt(comboBox1.Items.Count - 2);
        }
    }
Umut D.
  • 1,746
  • 23
  • 24
1

If I was doing this using MVVM, I would start with a ComboBox and modify it to suit.

You can get this almost for free if you use the built in ComboBox in DevExpress. Simply fill the dropdown list up with items that you want to auto-complete, then set the options for:

  • Auto dropdown (so when you start to type, it will automatically drop down a list of matches).
  • Filter list by match (i.e. the only items in the dropdown will be the ones that match what you have typed).
  • Match on partial (i.e. what you typed will filter the dropdown list, with a match anywhere, even in the centre).

If you wanted to get fancier, you could write a service that listens to what the user has currently typed in the box, then adjust the list of dropdown items to suit. Any items in the dropdown list that matches what the user types will automatically be displayed. I would use Reactive Extensions (RX) and Throttle to do this, see:

Community
  • 1
  • 1
Contango
  • 76,540
  • 58
  • 260
  • 305
  • For the record, I have no affiliation with `DevExpress`, it's just the one I'm familiar with. Telerik is also a very fine library, and no doubt they have something similar. – Contango Jun 25 '15 at 21:54