Problem using collection base and listbox

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi to all,

I have a class (persons) that derives from collection base:

and another class (person) with this properties:
-ID
-Name

When i have complete filled the object Persons with all the persons, i put
each persons inside a ListBox following this:

this.ListBoxpersons.DataSource=AvailableApplications;
this.ListBoxpersons.ValueMember="ID";
this.ListBoxpersons.DisplayMember="Name";
this.ListBoxPersons.SelectedIndex=-1;

My problem comes when i want to delete a person from the listbox and from
the collection of course.

and to do that i make this:

//Take the object to remove from collection
objPersons.Delete(int.Parse(ListBoxPersons.SelectedIndex.ToString()));

//Then refresh the listbox
ListBoxPersons.DataSource=objPersons;
ListBoxPersons.ValueMember="ID";
ListBoxPersons.DisplayMember="Name";
ListBoxPersons.SelectedIndex=-1;

This works, except when i try to delete the last item of the ListBox. I dont
know why but i obtain an Exception of type

System.ArgumentOutRangeException, index was out of range. Must be
non-negative and less than the size of the colletion.

I would like to know if this way its the correct one to refresh a list box.

Thanks
Regards.
Josema
 
Hi to all,

I have a class (persons) that derives from collection base:

and another class (person) with this properties:
-ID
-Name

When i have complete filled the object Persons with all the persons, i put
each persons inside a ListBox following this:

this.ListBoxpersons.DataSource=AvailableApplications;
this.ListBoxpersons.ValueMember="ID";
this.ListBoxpersons.DisplayMember="Name";
this.ListBoxPersons.SelectedIndex=-1;

My problem comes when i want to delete a person from the listbox and from
the collection of course.

and to do that i make this:

//Take the object to remove from collection
objPersons.Delete(int.Parse(ListBoxPersons.SelectedIndex.ToString()));

//Then refresh the listbox
ListBoxPersons.DataSource=objPersons;
ListBoxPersons.ValueMember="ID";
ListBoxPersons.DisplayMember="Name";
ListBoxPersons.SelectedIndex=-1;

This works, except when i try to delete the last item of the ListBox. I dont
know why but i obtain an Exception of type

System.ArgumentOutRangeException, index was out of range. Must be
non-negative and less than the size of the colletion.

I would like to know if this way its the correct one to refresh a list box.

Thanks
Regards.
Josema


Initially, it seems you're trying sending an integer value larger than
the new size of your Persons Collection. Let say, you have 10 Person
objects in your Person Collection and you delete #3 in the ListBox. If
you instruct your ListBox to delete the last Person object, does it
send the value 8 or 9 to your call:

objPersons.Delete(int.Parse(ListBoxPersons.SelectedIndex.ToString()));

If that Item in the ListBox is still being represented by 9, then you
will get that error:

System.ArgumentOutRangeException

because there wont be 10 objects in your collection, anymore, and the
highest index will be 8. Any attempt to Delete an index above 8 will
throw that exception.

It would be strange if the ListBox isn't refreshing its last index to
reflect the new size of the Persons Collection.

Please report, if something else seems to be the culprit.


Michael Hughes - Silverton, Oregon
http://wou.ath.cx/Trivia
http://wou.ath.cx/AmateurRadio
 
Back
Top