Error while adding Items in Combobox with auto complete

PARTH DESAI 61 Reputation points
2021-04-23T18:52:09.65+00:00

Hello,

I have a list of string having 10000 Items in it.
I am trying to add this in combobox with auto complete as Custom Source.
I wanted to add this items in backgroundworker but Its getting error like below

System.InvalidcastException: Interface not registered.

Below is the piece of code to reproduce

> using System;  
> using System.Collections.Generic;  
> using System.ComponentModel;  
> using System.Data;  
> using System.Drawing;  
> using System.Linq;  
> using System.Text;  
> using System.Threading.Tasks;  
> using System.Windows.Forms;  
>   
> namespace WindowsFormsApp1  
> {  
>  public partial class Form1 : Form  
>  {  
>  public Form1()  
>  {  
>  InitializeComponent();  
>  CheckForIllegalCrossThreadCalls = false;  
>  }  
>   
>  private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)  
>  {  
>  try  
>  {  
>  BindListInComboBox(comboBox1);  
>  }  
>  catch (Exception ex)  
>  {  
>  MessageBox.Show(ex.ToString());  
>  }  
>  }  
>   
>  private void BindListInComboBox(ComboBox comboBox)  
>  {  
>  List<string> myList = new List<string>() { "A", "B", "AB", "AC", "DB" };  
>  var autoComplete = new AutoCompleteStringCollection();  
>  autoComplete.AddRange(myList.ToArray());  
>  comboBox.AutoCompleteCustomSource = autoComplete;  
>  comboBox.SelectedIndex = -1;  
>  }  
>   
>  private void Form1_Load(object sender, EventArgs e)  
>  {  
>  backgroundWorker1.RunWorkerAsync();  
>  }  
>  }  
> }  

90882-image.png

Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2021-04-23T22:58:02.007+00:00

    See if this can work for you.

    public class Operations  
    {  
        /// <summary>  
        /// Mock up of data  
        /// </summary>  
        /// <returns></returns>  
        public static List<string> Items() => Enumerable.Range(1, 10100).Select(x => $"{x} item").ToList();  
    }  
    

    Form code

    public partial class Form1 : Form  
    {  
        public Form1()  
        {  
            InitializeComponent();  
      
            Shown += OnShown;  
        }  
          
        private async void OnShown(object sender, EventArgs e)  
        {  
            comboBox1.AutoCompleteMode = AutoCompleteMode.Suggest;  
            comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;  
      
            await Task.Factory.StartNew(() =>  
            {  
                Invoke(new MethodInvoker(() => comboBox1.DataSource = Operations.Items()));  
            });  
      
            comboBox1.SelectedIndex = -1;  
        }  
    }  
    

    ---
    91032-kpmvp.png

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.