List control acting up when last item is removed

D

David

I am using an ArrayList to populate a list control. Let's say the arraylist
has instances of a class called Thing. This has two properties called
ThingValue, and ThingName.

Here is how I bound the list with the arraylist

private void bindList()
{
listBox1.DataSource = null;
listBox1.DisplayMember = "ThingName";
listBox1.ValueMember = "ThingValue";
listBox1.DataSource = arrayList;
}

So the list gets populated as expected. Then I have an event handler for a
button to remove a selected item.

private void btnRemove_Click(object sender, System.EventArgs e)
{
arrayList.RemoveAt(listBox1.SelectedIndex);
bindList();
}

It works without a problem until I remove the very last item. When the last
item is removed, the list control has rows of

MyNameSpace.Thing
MyNameSpace.Thing
MyNameSpace.Thing
MyNameSpace.Thing


Then when an item is selected, the program crashes with the following error
message.

"Index was out of range. Must be non-negative and less than the size of the
collection.\r\nParameter name: index"

I have not been able to figure out how to fix it. My guess is that
SelectedIndex of ListControl is now out of range. But attempting to set the
SelectedIndex to 0 does not address the problem
 
C

Claes Bergefall

It crashes because the SelectedIndex property is -1
after you have removed the last item. And -1 is
not valid as input to ArrayList.RemoveAt

Not sure why you get a list containing
MyNameSpace.Thing
MyNameSpace.Thing
MyNameSpace.Thing
MyNameSpace.Thing
after you have removed the last item though.
When I try it I get an empty list, as expected.

Try changing the code in your button click handler to
arrayList.Remove(listBox1.SelectedItem);
instead and see if it helps. That will at least get rid
of the exception.

/claes
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi David,

If Claes' suggestion doesn't help, can you, pls, post some working sample in
order to have some code to work on.
 
G

Guest

You might need to assign a key field prior to binding.

Stoitcho Goutsev (100) said:
Hi David,

If Claes' suggestion doesn't help, can you, pls, post some working sample in
order to have some code to work on.

--

Stoitcho Goutsev (100) [C# MVP]


David said:
I am using an ArrayList to populate a list control. Let's say the
arraylist
has instances of a class called Thing. This has two properties called
ThingValue, and ThingName.

Here is how I bound the list with the arraylist

private void bindList()
{
listBox1.DataSource = null;
listBox1.DisplayMember = "ThingName";
listBox1.ValueMember = "ThingValue";
listBox1.DataSource = arrayList;
}

So the list gets populated as expected. Then I have an event handler for
a button to remove a selected item.

private void btnRemove_Click(object sender, System.EventArgs e)
{
arrayList.RemoveAt(listBox1.SelectedIndex);
bindList();
}

It works without a problem until I remove the very last item. When the
last item is removed, the list control has rows of

MyNameSpace.Thing
MyNameSpace.Thing
MyNameSpace.Thing
MyNameSpace.Thing


Then when an item is selected, the program crashes with the following
error message.

"Index was out of range. Must be non-negative and less than the size of
the collection.\r\nParameter name: index"

I have not been able to figure out how to fix it. My guess is that
SelectedIndex of ListControl is now out of range. But attempting to set
the SelectedIndex to 0 does not address the problem
 
S

Sean

I got exactly the same error using vb.net that I posted in
"microsoft.public.dotnet.languages.vb.controls" group. I have not got any
answer yet. Hope some experts can help us out.

--

Talespinner said:
You might need to assign a key field prior to binding.

Stoitcho Goutsev (100) said:
Hi David,

If Claes' suggestion doesn't help, can you, pls, post some working sample in
order to have some code to work on.

--

Stoitcho Goutsev (100) [C# MVP]


David said:
I am using an ArrayList to populate a list control. Let's say the
arraylist
has instances of a class called Thing. This has two properties called
ThingValue, and ThingName.

Here is how I bound the list with the arraylist

private void bindList()
{
listBox1.DataSource = null;
listBox1.DisplayMember = "ThingName";
listBox1.ValueMember = "ThingValue";
listBox1.DataSource = arrayList;
}

So the list gets populated as expected. Then I have an event handler for
a button to remove a selected item.

private void btnRemove_Click(object sender, System.EventArgs e)
{
arrayList.RemoveAt(listBox1.SelectedIndex);
bindList();
}

It works without a problem until I remove the very last item. When the
last item is removed, the list control has rows of

MyNameSpace.Thing
MyNameSpace.Thing
MyNameSpace.Thing
MyNameSpace.Thing


Then when an item is selected, the program crashes with the following
error message.

"Index was out of range. Must be non-negative and less than the size of
the collection.\r\nParameter name: index"

I have not been able to figure out how to fix it. My guess is that
SelectedIndex of ListControl is now out of range. But attempting to set
the SelectedIndex to 0 does not address the problem
 

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