打开一个窗体后,不做任何操作,点击右上角的红叉来关闭,出异常“索引超出范围。必须为非负值并小于集合大小。”。
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing); //异常出现在这句上!
        }自已试了一下,发现只要点击过tabControl的tabPage2就不会出现上述情况。(窗体初始显示的是tabPage1)
tabPage2上面有很多东西:button、groupBox、label、radioButton、pictureBox、checkBox、dataGridView。我大概错在哪了?请给我一个方向,谢谢!

解决方案 »

  1.   

     protected override void Dispose(bool disposing)
            {
                if (disposing)
                {
                    if (components != null)
                    {
                        components.Dispose();
                    }
                }
                base.Dispose(disposing);
            }
      

  2.   

    我找到了,把以下代码屏蔽掉,就不会出现上述问题。
                //DataGridViewColumn col13 = new DataGridViewButtonColumn();//'建立一个新的列
                //col13.DisplayIndex = dataGridView2.Columns[13].DisplayIndex;//并设置其属性与要改变的列相同
                //col13.DataPropertyName = dataGridView2.Columns[13].DataPropertyName;
                //col13.HeaderText = dataGridView2.Columns[13].HeaderText;
                //col13.Name = dataGridView2.Columns[13].Name;
                //dataGridView2.Columns.RemoveAt(13);//'除去要改变的列
                //dataGridView2.Columns.Insert(13, col13);//'添加新的列
    因为tabPage2中有一个dataGridView2;其中的Columns[13]想是DataGridViewButtonColumn样式的,所以写了这段代码。现在是没有这段代码是不会出问题;
    有这段代码,但打开窗体后,点击出tabPage2后关闭窗体也不会出现问题。
    有这段代码,且打开窗体后,立即关闭窗体会出现问题。(打开窗体默认是tabPage1显示)
      

  3.   

    光看这段代码也看不出问题,你把StackTrace贴下看
      

  4.   

    应该在程序加载的时候把TabContol的每一个page激活一下,
      

  5.   

    我开始也是这样想的,但感觉这个方法不是长久之计,好像没有从根上去解决问题。
    老大,我一直没敢回你的话。因为StackTrace我不会用,呵呵。
    看了你回贴后,特意跑到msdn上想学一下StackTrace的用法,学的晕乎乎的,没学明白。
    惭愧,惭愧,没有系统的学习就是不行啊!
      

  6.   

    StackTrace就是抛出未处理异常时那一长串调用方法的堆栈
    你调试时出现异常的时候,可以在编译器里看到异常的详细信息,里面找一下StackTrace就能看到。或者你catch了异常后MessageBox.Show(e.StackTrace)看看
      

  7.   


       在 System.Collections.ArrayList.get_Item(Int32 index)
       在 System.Windows.Forms.DataGridViewColumnCollection.get_Item(Int32 index)
       在 System.Windows.Forms.DataGridView.FlushDisplayedChanged()
       在 System.Windows.Forms.DataGridView.PerformLayoutPrivate(Boolean useRowShortcut, Boolean computeVisibleRows, Boolean invalidInAdjustFillingColumns, Boolean repositionEditingControl)
       在 System.Windows.Forms.DataGridView.ResetUIState(Boolean useRowShortcut, Boolean computeVisibleRows)
       在 System.Windows.Forms.DataGridView.OnColumnCollectionChanged_PreNotification(CollectionChangeEventArgs ccea)
       在 System.Windows.Forms.DataGridViewColumnCollection.OnCollectionChanged(CollectionChangeEventArgs ccea, Boolean changeIsInsertion, Point newCurrentCell)
       在 System.Windows.Forms.DataGridViewColumnCollection.Clear()
       在 System.Windows.Forms.DataGridView.Dispose(Boolean disposing)
       在 System.ComponentModel.Component.Dispose()
       在 System.Windows.Forms.Control.Dispose(Boolean disposing)
       在 System.ComponentModel.Component.Dispose()
       在 System.Windows.Forms.Control.Dispose(Boolean disposing)
       在 System.Windows.Forms.TabControl.Dispose(Boolean disposing)
       在 System.ComponentModel.Component.Dispose()
       在 System.Windows.Forms.Control.Dispose(Boolean disposing)
       在 System.Windows.Forms.ContainerControl.Dispose(Boolean disposing)
       在 System.Windows.Forms.Form.Dispose(Boolean disposing)
       在 HB_Class.HBFormA.Dispose(Boolean disposing) 位置 C:\Users\LIUBO\Desktop\HB_C#_A018.5\HB_ClassLibrary\HBFormA.Designer.cs:行号 20
       在 HB_Class.HBFormA_X2_A.Dispose(Boolean disposing) 位置 C:\Users\LIUBO\Desktop\HB_C#_A018.5\HB_ClassLibrary\HBFormA_X2_A.Designer.cs:行号 20
       在 HB_WindowsFormsApplication.Form进货单.Dispose(Boolean disposing) 位置 C:\Users\LIUBO\Desktop\HB_C#_A018.5\HB_WindowsFormsApplication\Form进货单.Designer.cs:行号 20
       在 System.ComponentModel.Component.Dispose()
       在 System.Windows.Forms.Form.WmClose(Message& m)
       在 System.Windows.Forms.Form.WndProc(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       在 System.Windows.Forms.UnsafeNativeMethods.DefMDIChildProc(IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       在 System.Windows.Forms.Form.DefWndProc(Message& m)
       在 System.Windows.Forms.Control.WndProc(Message& m)
       在 System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       在 System.Windows.Forms.ContainerControl.WndProc(Message& m)
       在 System.Windows.Forms.Form.WmSysCommand(Message& m)
       在 System.Windows.Forms.Form.WndProc(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       在 System.Windows.Forms.UnsafeNativeMethods.DefMDIChildProc(IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       在 System.Windows.Forms.Form.DefWndProc(Message& m)
       在 System.Windows.Forms.Control.WndProc(Message& m)
       在 System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       在 System.Windows.Forms.ContainerControl.WndProc(Message& m)
       在 System.Windows.Forms.Form.WmNcButtonDown(Message& m)
       在 System.Windows.Forms.Form.WndProc(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.Run(Form mainForm)
       在 HB_WindowsFormsApplication.Program.Main() 位置 C:\Users\LIUBO\Desktop\HB_C#_A018.5\HB_WindowsFormsApplication\Program.cs:行号 18
       在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       在 System.Threading.ThreadHelper.ThreadStart()
      

  8.   

     在 System.Collections.ArrayList.get_Item(Int32 index)
      在 System.Windows.Forms.DataGridViewColumnCollection.get_Item(Int32 index)看这两行,有可能是DGV  的 数据绑定造成的。
      

  9.   

    好像是比较奇怪
    //DataGridViewColumn col13 = new DataGridViewButtonColumn();//'建立一个新的列
                //col13.DisplayIndex = dataGridView2.Columns[13].DisplayIndex;//并设置其属性与要改变的列相同
                //col13.DataPropertyName = dataGridView2.Columns[13].DataPropertyName;
                //col13.HeaderText = dataGridView2.Columns[13].HeaderText;
                //col13.Name = dataGridView2.Columns[13].Name;
                //dataGridView2.Columns.RemoveAt(13);//'除去要改变的列
                //dataGridView2.Columns.Insert(13, col13);//'添加新的列
    这段代码是写在哪里的?
      

  10.   


    写在窗体的Shown里面,试过放在Load里面,也不行。