Hi Peri,
I'd recommend either caching the control with the 20,000 preloaded
provided:
i) The 6MB requirement (approx. memory used for control with 20,000
names) isn't an issue
ii) You've an algorithm for loading additional (newly entered) names
so as to avoid reloading all the data following initial population
OR
The example code below manages to do what I think you'd like to do by
reloading the data from scratch. It does take significantly longer
than 3 to 4 seconds because the combo box is updated following each
addition but there are some results immediately and the UI remains
responsive.
[Note: I've not tried to find out whether autocompletion prompting can
be enabled before the combo box has been fully populated. Anyone?]
public class UsersDisplayObject
{
public BindingList<User> Users;
public ComboBox UsersDisplay;
public UsersDisplayObject(BindingList<User> Users, ComboBox
UsersDisplay)
{
this.Users = Users;
this.UsersDisplay = UsersDisplay;
}
}
public class User
{
private int id;
private string userName;
public int ID
{
get { return id; }
}
public string UserName
{
get { return userName; }
}
public User(int ID, string UserName)
{
this.id = ID;
this.userName = UserName;
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace Tzarpho.ComboBoxLoadingExample
{
public partial class Form1 : Form
{
BindingList<User> users = new BindingList<User>();
delegate void
AddedUserEventHandler(AutoCompleteStringCollection
AutoCompleteSource);
event AddedUserEventHandler AddedUserComplete;
Thread addingUsersThread;
delegate void AddUserCallback(UsersDisplayObject
DataDisplayObject);
public Form1()
{
InitializeComponent();
InitializeData();
this.AddedUserComplete += new
AddedUserEventHandler(OnUserAdded);
}
private void OnUserAdded(AutoCompleteStringCollection
AutoCompleteSource)
{
comboBox1.AutoCompleteCustomSource = AutoCompleteSource;
Application.DoEvents();
}
private void InitializeData()
{
users.Clear();
for (int i = 1; i < 20000; i++)
{
users.Add(new User(i, string.Format("Example Name
{0}", i)));
}
}
private void button1_Click(object sender, EventArgs e)
{
addingUsersThread = new Thread(new
ParameterizedThreadStart(this.AddUsersSafely));
addingUsersThread.Start(new UsersDisplayObject(users,
comboBox1));
}
private void AddUsers(object DataDisplayObject)
{
Thread.CurrentThread.IsBackground = true;
UsersDisplayObject dataDisplayObject =
(UsersDisplayObject) DataDisplayObject;
AutoCompleteStringCollection autocompleteSource = new
AutoCompleteStringCollection();
BindingList<User> Users = dataDisplayObject.Users;
ComboBox UsersDisplay = dataDisplayObject.UsersDisplay;
UsersDisplay.ValueMember = "ID";
UsersDisplay.DisplayMember = "UserName";
foreach (User user in Users)
{
UsersDisplay.Items.Add(user);
autocompleteSource.Add(user.UserName);
AddedUserComplete(autocompleteSource);
}
}
private void AddUsersSafely(object DataDisplayObject)
{
UsersDisplayObject dataDisplayObject =
(UsersDisplayObject)DataDisplayObject;
BindingList<User> Users = dataDisplayObject.Users;
ComboBox UsersDisplay = dataDisplayObject.UsersDisplay;
if (UsersDisplay.InvokeRequired)
{
AddUserCallback addUser = new
AddUserCallback(AddUsersSafely);
this.Invoke(addUser, new object[]
{ dataDisplayObject });
}
else
{
AddUsers(dataDisplayObject);
}
}
}
Kofi Sarfo