ListBox crash when accessing SelectedIndex property.

V

Valerie Hough

Can anyone please tell me how it is possible to get the following crash when
accessing ListBox.SelectedIndex property:

Message: Index was outside the bounds of the array.

Stack Trace:
at System.Windows.Forms.ListBox.ItemArray.GetEntryObject( Int32
virtualIndex, Int32 stateMask)
at System.Windows.Forms.ListBox.get_SelectedIndex()

It is not possible to use the ListBox.SelectedIndex = <some bad value> to
put a bad value in there (I've tried and this causes an immediate
exception),
so I just can't see how the above crash is possible.

The list box is sometimes set to be SelectionMode of MultiExtended, but the
documentation does not say SelectedIndex is invalid with this mode.

Can anyone please help?

Thanks in advance,
Valerie Hough
 
J

Jeff Gaines

Can anyone please tell me how it is possible to get the following crash
when
accessing ListBox.SelectedIndex property:

I have seen this when no item is actually selected, I always check that
SelectedItems.Count > 0 in my own code.

If I remember correctly Selected Index Changed gets called twice when a
selection changes, once to clear the selection and then again to select
the new item which is why I put my check in.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top