3
Answers

OutOfRange Exception from ArrayList

Jyotsna.Verma

Jyotsna.Verma

20y
3.2k
1
I have a ListBox and set its Datasource to an ArrayList, If I remove the last item in the ArrayList and then click on the ListBox I get an outOfRange Exception. I have copied the code which removes the item from the ArralList and error below. Any help would be greatly appreciated: Code ------ private void butRemove_Click(object sender, System.EventArgs e) { //g_InterestedPartiesArray is the ArrayList this.bChangesMade=true; BLUser blUser = new BLUser(); int nLoop=0; foreach (BLUser row in this.g_InterestedPartiesArray) { blUser=row as BLUser; if (blUser.Pid==System.Convert.ToInt32(this.lbInterestedPeople.SelectedValue)) { this.g_InterestedPartiesArray.RemoveAt(nLoop); break; } nLoop++; } //Refresh the listbox with the changed data in the ArrayList this.lbInterestedPeople.DataSource=null; this.lbInterestedPeople.DataSource=this.g_InterestedPartiesArray; this.lbInterestedPeople.DisplayMember="FullName"; this.lbInterestedPeople.ValueMember="Pid"; } Error ------ Error : Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index Source : mscorlib Stack Trace : at System.Collections.ArrayList.get_Item(Int32 index) at System.Windows.Forms.CurrencyManager.EndCurrentEdit() at System.Windows.Forms.CurrencyManager.ChangeRecordState(Int32 newPosition, Boolean validating, Boolean endCurrentEdit, Boolean firePositionChange, Boolean pullData) at System.Windows.Forms.CurrencyManager.set_Position(Int32 value) at System.Windows.Forms.ListBox.OnSelectedIndexChanged(EventArgs e) at System.Windows.Forms.ListBox.WmReflectCommand(Message& m) at System.Windows.Forms.ListBox.WndProc(Message& m) at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, IntPtr wParam, IntPtr lParam) at System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd, Message& m) at System.Windows.Forms.Control.WmCommand(Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ScrollableControl.WndProc(Message& m) at System.Windows.Forms.ContainerControl.WndProc(Message& m) at System.Windows.Forms.ParkingWindow.WndProc(Message& m) at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam) at System.Windows.Forms.NativeWindow.DefWndProc(Message& m) at System.Windows.Forms.Control.DefWndProc(Message& m) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ListBox.WndProc(Message& m) at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMethods+IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.RunDialog(Form form) at System.Windows.Forms.Form.ShowDialog(IWin32Window owner) at System.Windows.Forms.Form.ShowDialog() at Quest.Applic.APUpdateInternalAuditGUI..ctor(BLInternalAudit blInternalAudit, Boolean readOnly) in h:\userdata\c#\quest\quest.applic\apupdateinternalauditgui.cs:line 227 at Quest.Applic.APStartupGUI.dgAudits_DoubleClick1(Object sender, EventArgs e) in h:\userdata\c#\quest\quest.applic\apstartupgui.cs:line 1293
Answers (3)