UNKNOWN PROBLEM (ListBox DataSource )
-
I have a ListBox (CatList) and an ArrayList (Cats) bound to it. I'm removing the selected item in the ListBox from the ArrayList and rebinding the source after removing:
private ArrayList Cats = new ArrayList(); //The arraylist source for the ListBox ... //some items are added here ...
private void bRemoveCat_Click(object sender, System.EventArgs e) { int selIndex = this.CatList.SelectedIndex; this.CatList.DataSource = null; this.CatList.Items.Clear(); Cats.RemoveAt(selIndex); this.CatList.DataSource = Cats; this.CatList.DisplayMember = "Name"; this.CatList.ValueMember = "ID"; }
No exception is thrown when the above code is executed but after the code is executed, if I click on the ListBox, the exception below is immediately thrown.An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll Additional information: Index was out of range. Must be non-negative and less than the size of the collection.
What do you think? Thanks in advance. Radgar "Imagination is more important than knowledge." - Albert Einstein -
I have a ListBox (CatList) and an ArrayList (Cats) bound to it. I'm removing the selected item in the ListBox from the ArrayList and rebinding the source after removing:
private ArrayList Cats = new ArrayList(); //The arraylist source for the ListBox ... //some items are added here ...
private void bRemoveCat_Click(object sender, System.EventArgs e) { int selIndex = this.CatList.SelectedIndex; this.CatList.DataSource = null; this.CatList.Items.Clear(); Cats.RemoveAt(selIndex); this.CatList.DataSource = Cats; this.CatList.DisplayMember = "Name"; this.CatList.ValueMember = "ID"; }
No exception is thrown when the above code is executed but after the code is executed, if I click on the ListBox, the exception below is immediately thrown.An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll Additional information: Index was out of range. Must be non-negative and less than the size of the collection.
What do you think? Thanks in advance. Radgar "Imagination is more important than knowledge." - Albert EinsteinI'd verify the value of
selIndex
before you try and use it on the Cats arraylist. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome -
I'd verify the value of
selIndex
before you try and use it on the Cats arraylist. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome