ListBox && 'System.IndexOutOfRangeException'

D

Dave H

1) Start a new project.
2) Add a ListBoxto the form.
3) Add a few items to the ListBox.
4) Change the ListBox's SelectionMode to one of the multi select modes.
5) In the MouseDown event of the ListBox add some code to use
SelectedIndex..

eg int idx = listBox1.SelectedIndex ;

I get the exception :

An unhandled exception of type 'System.IndexOutOfRangeException' occurred in
system.windows.forms.dll
Additional information: Index was outside the bounds of the array.

and the top part of my call stack looks like :

system.windows.forms.dll!ItemArray.GetEntryObject(int virtualIndex = 0, int
stateMask = 1) + 0x41 bytes
system.windows.forms.dll!System.Windows.Forms.ListBox.SelectedObjectCollecti
on.GetObjectAt(int index = 0) + 0x23 bytes
system.windows.forms.dll!System.Windows.Forms.ListBox.get_SelectedIndex() +
0x95 bytes
WindowsApplication8.exe!WindowsApplication8.Form1.listBox1_MouseDown(System.
Object sender = {System.Windows.Forms.ListBox},
System.Windows.Forms.MouseEventArgs e = {X=17 Y=23 Button=Left}) Line 97 +
0xe bytes

I've been Googling but not had much luck, this post describes the same
problem :

Deja Vu - Trouble with windows forms event model (ListBox in particular)
http://groups.google.co.uk/[email protected]

Is this a bug?

Has anyone else encountered this problem? (or found a fix!)

I need to be able to insert items into my ListBox on MouseDown, what hope do
I have of that if it is in some sort of transient state. :(

David
 
M

Morten Wennevik

Hi Dave,

I just checked, and I get the same error. No matter which item I select
it causes an IndexOutOfBoundException. Odd.

using

ListBox.SelectedIndexCollection coll = listBox1.SelectedIndices;
if(coll.count > 0)
{
// coll[0] will also return IndexOutOfBoundsException
}

No solution though :(

Morten Wennevik [C# MVP]
 

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