none
Index was out of range. Must be non-negative and less than the size of the collection. RRS feed

  • Question

  • Problem 

     The Problem:

    Index was out of range. Must be non-negative and less than the size of the collection.

    Parameter name: index



    Problem Location:

    Forms.DataForm.Dispose(Boolean disposing) in D:\TFSServerCollection\Module \Code\Forms\Forms\DataForm.designer.cs:line 23

    error on line 23 base.dispose(disposing);

      protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
    
                try
                {
                    base.Dispose(disposing); line 23
                }
                catch(System.ArgumentOutOfRangeException ex)
                {
                    xExceptions.ShowExceptionInfo(ex);
                }
            }

    code that make this error 

     public static void frm_Closing(object sender, FormClosingEventArgs e)
        {
            if (e.Cancel == true) return;
    
            xForm frm = sender as xForm;
    
            if (!frm.StopSaveGridColumnsWidth) xLayout.SaveGridColumnsWidth(frm.Controls);
            MenuManager.RemoveFromActiveWindowsMenu(frm);
            GlobalVariables.ActiveFormName = "";
            GlobalVariables.ActiveFormID = -1;
    
            
            if (SqlFactory.FormsData.Keys.Contains(frm.TableName))
            {
                SqlFactory.FormsData.Remove(frm.TableName);
            }
    
            ApplicationOpenForms.RemoveForm(frm.Name);
        }here error generated

    After line ApplicationOpenForms.RemoveForm(frm.Name);

    error generated 

    function removeform

     public static void RemoveForm(string FormName)
        {
            if (IsExist(FormName))
            {
                int count = 0;
                for (int i = 0; i < Application.OpenForms.Count; i++)
                {
                    if (Application.OpenForms[i].Name == FormName)
                    {
                        count += 1;
                    }
                }
                if (count == 1)
                    Forms.Remove(FormName);
            }
        }


    Sunday, May 26, 2019 7:28 PM

All replies

  • can you help me in that problem
    Monday, May 27, 2019 2:58 AM
  • can you help me in that problem
    public static void RemoveForm(string FormName)
        {
            if (IsExist(FormName))
            {
                int count = 0;
                for (int i = 0; i < Application.OpenForms.Count; i++)
                {
                    if (Application.OpenForms[i].Name == FormName)
                    {
                        count += 1;
                    }
                }
                if (count == 1)
                    Forms.Remove(FormName);
            }
        }

    there is something hoaky about the double counters logic and using the index. When indexing and access a collection of items in a collection via an index it is 0 based.  If the index goes below 0 a negative number the index is out of range of items in the collection, becuase there is no item in the collection below first item in the collectionthe 0 indexed item. If the index goes above the max number of items in the collection 0 based, then it's index out of range.

    If you know how to use the Visual Stuido debugger, then debug the code by setting a breakpoint and start single-stepping code and use Quickwatch to look at the index count and the collection.count.


    • Edited by DA924x Monday, May 27, 2019 4:47 AM
    Monday, May 27, 2019 3:55 AM
  • yes i know how to debug

    but please tell me which line i will make breakpoint and what collection i debug

    can you tell me which line i debug and collection please

    Monday, May 27, 2019 4:24 AM
  • yes i know how to debug

    but please tell me which line i will make breakpoint and what collection i debug

    can you tell me which line i debug and collection please

    Application.OpenForms is a collection of form objects in a collection The fact that it has a Count property should give you a clue that the object is a collection or a list , a list of items.

    Also object names that end with 's' a plural naming convention or the object name contains the word 'list', like OpenFromList is a clue that object is collection or list.

    As far as where to put the breakpoint,  put it on the 'if (IsExist(FormName))' line and start single-stepping until it blows up.

    Make sure you know what the collection.Count is ,  what the index count is and the counter 'count' is at the time the code blows-up, use Quickwatch.


    • Edited by DA924x Monday, May 27, 2019 4:43 AM
    Monday, May 27, 2019 4:43 AM
  • thank you for reply

    ApplicationOpenForms.count from add watch is have 3 then after steps into 

    ApplicationOpenForms.remove(form)

    it become 2

    then from stacktrace i get error as following

       at System.Collections.ArrayList.get_Item(Int32 index)
       at System.Windows.Forms.DataGridViewColumnCollection.get_Item(Int32 index)
       at System.Windows.Forms.DataGridView.PositionEditingControl(Boolean setLocation, Boolean setSize, Boolean setFocus)
       at System.Windows.Forms.DataGridView.PerformLayoutPrivate(Boolean useRowShortcut, Boolean computeVisibleRows, Boolean invalidInAdjustFillingColumns, Boolean repositionEditingControl)
       at System.Windows.Forms.DataGridView.ResetUIState(Boolean useRowShortcut, Boolean computeVisibleRows)
       at System.Windows.Forms.DataGridViewRowCollection.OnCollectionChanged_PreNotification(CollectionChangeAction cca, Int32 rowIndex, Int32 rowCount, DataGridViewRow& dataGridViewRow, Boolean changeIsInsertion)
       at System.Windows.Forms.DataGridViewRowCollection.OnCollectionChanged(CollectionChangeEventArgs e, Int32 rowIndex, Int32 rowCount, Boolean changeIsDeletion, Boolean changeIsInsertion, Boolean recreateNewRow, Point newCurrentCell)
       at System.Windows.Forms.DataGridViewRowCollection.ClearInternal(Boolean recreateNewRow)
       at System.Windows.Forms.DataGridView.OnClearingColumns()
       at System.Windows.Forms.DataGridViewColumnCollection.Clear()
       at System.Windows.Forms.DataGridView.Dispose(Boolean disposing)
       at System.ComponentModel.Component.Dispose()
       at System.Windows.Forms.Control.Dispose(Boolean disposing)
       at System.ComponentModel.Component.Dispose()
       at System.Windows.Forms.Control.Dispose(Boolean disposing)
       at System.Windows.Forms.TabControl.Dispose(Boolean disposing)
       at System.ComponentModel.Component.Dispose()
       at System.Windows.Forms.Control.Dispose(Boolean disposing)
       at System.Windows.Forms.Form.Dispose(Boolean disposing)
       at Forms.DataForm.Dispose(Boolean disposing) in D:\TFSServerCollection\TABSolutions\Module 3.0.0\Trunk\Code\Forms\Forms\DataForm.designer.cs:line 25
    so how to solve that problem please 


    Sunday, June 30, 2019 1:09 AM
  • AllI can tell you is to check the index number against the list.count number. If the index number goes below +0 the index number is a negative number or the index no goes higher than the list.count number, then stop what you are doing, becuase the index number is outside the range of items that can be accessed in the list by the index number.
    Sunday, June 30, 2019 1:43 PM