Click here to Skip to main content
15,891,777 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am working on a Time Converter, which has 2 Comboboxes, To and From.

Both Combobox have a BIG list of about 20K+ of countries and cities name.

This list is hard coded in the Code itself, and I'm using this code:

this.comboBox_TO.Items.AddRange(new object[]
{
 "Afghanistan, Kabul",
  //List
}

this.comboBox_FROM.Items.AddRange(new object[]
{
 "Afghanistan, Kabul",
  //List
}


The issue here is this code takes a lot of time to add the BIG list into Combobox, and resets everytime time converter is closed. So, next time I reopen Time converter, it takes lots of time again.

I need the help in either,

1) Fastest way to add list into Combobox Items, OR
2) Stop Combobox resetting everytime converter is reopened.

Thanks in advance.

What I have tried:

this.comboBox_TO.Items.AddRange(new object[]
{
 "Afghanistan, Kabul",
  //List
}

this.comboBox_FROM.Items.AddRange(new object[]
{
 "Afghanistan, Kabul",
  //List
}
Posted
Updated 28-Jun-18 15:17pm
Comments
Graeme_Grant 1-Mar-17 6:44am    
It is hard to give a helpful answer as we can't see the code that is used in your question.

Please update your Question with the missing code by clicking on Improve question.
Garth J Lancaster 1-Mar-17 6:44am    
can you use a hierarchical approach or a 'load on demand' approach ? load all the countries first, user selects country, then load cities for the country ?

(this doesn't actually answer your question except to suggest a different 'approach' than loading everything straight up)
[no name] 1-Mar-17 6:52am    
"Both Combobox have a BIG list of about 20K+", you do not have a performance issue, you have a design issue. No one is going to sift through a list of 20K items.
Jochen Arndt 1-Mar-17 7:15am    
You actually use the fastest way.

The only optimisation might be setting the Sorted property to false (if not already cleared).

To avoid re-initialisation you may hide the form containg the combo boxes instead of closing it.

But as already said:
A list with 20k+ items is not user friendly.
Try to break it down (e.g. one list with countries and a dependant list with towns in the selected country).
Afzaal Ahmad Zeeshan 1-Mar-17 8:09am    
How long it takes?

There is no good way to do that: I wouldn't - as a user - want to try and select a city from a dropdown with 20,000 items in it: heck, I wouldn't want to select from a drop down with 2,000 items!

Instead, consider using two DDLs: the first selects the country, and that action fills the second. That way you are presenting the user with the minimum "spurious" information needed to do their job.

I'd also consider using a database, CSV, or XML file for your data instead of hard coding the countries and cities: load the data from that into a datatable and you can use it as sources for the DDLs. Hard coding isn't generally a good idea for anything with more than a couple of items. This may help: Creating Cascading DropDownLists in ASP.Net[^]
 
Share this answer
 
Quote:
Both Combobox have a BIG list of about 20K+ of countries and cities name.

A ComboBox/DropBox with 20K+ entries is a bad idea from the beginning.

You need to find another way to choose in the list. Think about Google and how the drop list evolve as you type something.
just typing a single letter can make your list 10 times smaller.
 
Share this answer
 
Pre-allocate initial capacity using pinvoke to add items faster and then entire items at once using AddRange

private const int CB_INITSTORAGE = 0x161;

[DllImport("user32.dll", EntryPoint = "SendMessageA", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
public static extern int SendMessage(IntPtr handle, int uMsg, int wParam, object lParam);

private void btnTest_Click(object sender, EventArgs e)
{
   //populate values, replace this with your database values
   List<object> lst = new List<object>(10000);
   for (int i = 0; i < 1000; i++)
      lst.Add("Item" + i);

   //wParam = total items being populated
   //lParam = total bytes to allocate, items count * 100 (average 100 chars per item)
   SendMessage(comboBox2.Handle, CB_INITSTORAGE, lst.Count, lst.Count * 100);
   comboBox2.Items.AddRange(lst.ToArray());
}
 
Share this answer
 
v3
Comments
Dave Kreskowiak 28-Jun-18 22:59pm    
First, this is an old question that was already answered sufficiently.

Second, ANY control with 20,000 items in it is just stupid. In your example, you're using just 1,000 items. Even that would be very painful to use from a users perspective.

The only REAL answer is to rethink what the interface is doing and break this down to a far more usable list of entries and possibly even use a second Combo to narrow down possibilities.
Suntaurus 29-Jun-18 2:07am    
I know it is old post, I also know it does not make sense to add too many items for the user to select. Also, don't think it will be always only one drop down and one specific scenario. I have a couple of drop-downs that calibrate based on the selection and initially at least one drop down has 10000+ items.


I have only answered looking at the question. i.e. adding items faster.

You can please go ahead and delete my solution. if you think its useless.
Maciej Los 29-Jun-18 3:09am    
You can do it yourself... See right-bottom corner of your "answer".
Note: answering already answered questions may cause down-voting.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900