user3464522
user3464522

Reputation: 71

A faster search for combobox autocomplete

I got the following code from one of the posts, it's an autocomplete for a combobox. The problem is if I've a big array, the search seems to be lagging. Does anyone have a better solutions? Thanks...

P.S. This is my first post so please be gentle with me :)

    private void comboBox1_TextChanged(object sender, EventArgs e)
        {
          string item = comboBox1.Text;
          item = item.ToLower();
          comboBox1.Items.Clear();
          List<string> list = new List<string>();
          for (int i = 0; i < vocFiles.Length; i++)
          {
            if (vocFiles[i].ToLower().Contains(item))
              list.Add(vocFiles[i]);
          }
          if (item != String.Empty)
            foreach (string str in list)
              comboBox1.Items.Add(str);
          else
            comboBox1.Items.AddRange(vocFiles);
          comboBox1.SelectionStart = item.Length;
          comboBox1.DroppedDown = true;
        }
    }

Upvotes: 0

Views: 369

Answers (1)

Andrew
Andrew

Reputation: 2335

Try something like the following instead of looping through each item individually.

List<string> myList = vocFiles.Where (v => v.ToLower().Contains(item)).ToList();

Upvotes: 2

Related Questions