ListBox Performance - SetSelected

  • Thread starter Thread starter Wayne Hartell
  • Start date Start date
W

Wayne Hartell

Hi there,

I have the case where I am trying to optimize some slow code that selects
listBox items.

For approximately 6,500 items, the IndexOf method takes about 3 seconds on a
2.4GHz machine, and SetSelected takes about 9 seconds.

The label lookup from the lable map takes about 0.004 seconds.

Is there any way of selecting items in a quicker, possible batch fashion?
SelectedIndices appears to be read only. Surely it can't take 9 seconds to
select all these indexes when only 10 or 12 are displayed at any one time.
I've tried SuspendLayout() and ResumeLayout() to try to avoid code that
updates the display after every selection, but the performance remained the
same.

for(int i = 1; i <= aenumElements.Count; i++)
{
string alabel = m_idLabelMap[aenumElements] as string;
int aindex = listBoxElements.Items.IndexOf(alabel);
if(aindex >= 0)
listBoxElements.SetSelected(aindex, true);
}

Any tips would be greatly appreciated (short of re-working the use case to
not require 6,500 elements of course).

Thanks,
Wayne.
 
Weird to answer my own question, but I gained a significant performance
improvement (60 times, or 12 seconds down to 0.2 seconds) by using a
ListView control with one column of the appropriate width, and maintaining a
Hashtable of indexes. Would have been easier if the ListBox was faster, but
this will do.

Wayne.
 
Back
Top