question

PARTHDESAI-2292 avatar image
0 Votes"
PARTHDESAI-2292 asked karenpayneoregon edited

Error while adding Items in Combobox with auto complete

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


dotnet-csharp
image.png (24.9 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered karenpayneoregon edited

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



kpmvp.png (2.0 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.