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
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