I am populating a combobox with a list of names and the list contains approximately 7600 items.
Here is the code:

foreach (string customerName in customerNames)
{
    comboBoxCustName.Items.Add(customerName);
}

This loop takes nearly one minute to complete (58.6 seconds to be precise). I don't think it should take that long, can someone please offer some ideas as to why it's taking so long?

Edited 3 Years Ago by spowel4

I just solved this on my own, thought I'd post what I changed in case anyone else has this problem.
I moved the code from the form load event to immediately after the InitializeComponent() line. Now the combobox populates in about 1.5 seconds.

For future reference, if you're doing a cluster update like this, before you populate the control you should set Enabled = false; as it (stops it firing events) and SuspendLayout (if you're changing any control layout properties) do your updates and then call ResumeLayout and set Enabled = true; for the control.

I'm guessing that you had an event attached to your combo box causing it to fire everytime you added a value?

I have two events attached - KeyDown and SelectedIndexChanged. You're thinking one of these events was probably firing each time an item was added to the combobox? I saw somewhere where the combobox has to redraw itself every time an item is added and that slows it way down; their solution was to move the combobox population into the form constructor, which is what I did.

This question has already been answered. Start a new discussion instead.