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[i]] 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.

Re: ListBox Performance - SetSelected by Wayne

Wayne
Thu Jul 10 04:59:15 CDT 2003

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.

"Wayne Hartell" <whar@haestad.com> wrote in message
news:OICcJbrRDHA.1576@TK2MSFTNGP12.phx.gbl...
> 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[i]] 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.
>
>