I\'m wondering what would be the best way to populate a ListBox control of a WinForm, populated depending on a radio btn?
I have seen some suggestions to use a forea
You don't need to populate the ListBox
from in another thread. If you use a correct way to populate it, populating 10000 items takes a short time (for me 200-300 ms).
The part that you may want to put in another thread is loading data not adding data to ListBox
.
To add items to ListBox
it's enough to use AddRange:
this.listBox1.AddRange(array);
Which is equivalent to using below code. First call BeginUpdate method of ListBox
and then use a loop to Add
items to Items
collection and at last call EndUpdate:
this.listBox1.BeginUpdate();
foreach (var item in array)
{
this.listBox1.Items.Add(item);
}
this.listBox1.EndUpdate();
Take a look at source code of AddRange
method.