简体   繁体   English

在datagrid中选择一个单元格后出现ObjectDisposedException

[英]ObjectDisposedException after selecting a cell in datagrid

I'm getting ObjectDisposedException after I exit the form. 退出表单后,我得到了ObjectDisposedException The base form is my custom class that inherits from Windows.Forms.Form , and It works perfectly for my other 11 forms. 基本表单是我的自定义类,该类继承自Windows.Forms.Form ,并且对我的其他11种表单都非常适用。 Unfortunately, in this im getting this error ONLY after I selected a row in datagrid. 不幸的是,在这种情况下,只有在datagrid中选择了一行后才出现此错误。

The stacktrace: 堆栈跟踪:

at Microsoft.AGL.Common.MISC.HandleAr(PAL_ERROR ar)
at System.Windows.Forms.ScrollBar._GetInfo(AGL_SCROLLINFO_GRBIT grbit, AGL_SCROLLINFO& scrollinfo)
at System.Windows.Forms.ScrollBar.get_Value()
at System.Windows.Forms.GridRenderer._GetRectAt(Int32 irow, Int32 icol)
at System.Windows.Forms.GridRenderer._DrawCells(Graphics gx)
at System.Windows.Forms.GridRenderer._RenderCell(Graphics gxControl, Int32 irow, Int32 icol)
at System.Windows.Forms.GridRenderer._RenderCell(Int32 irow, Int32 icol)
at System.Windows.Forms.DataGrid._FocusChanged()
at System.Windows.Forms.DataGrid.OnLostFocus(EventArgs e)
at System.Windows.Forms.Control.WnProc(WM wm, Int32 wParam, Int32 lParam)
at System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam)
at Microsoft.AGL.Forms.WL.Destroy(IntPtr hwnThis)
at System.Windows.Forms.Control.Dispose(Boolean disposing)
at System.Windows.Forms.DataGrid.Dispose(Boolean disposing)
at System.ComponentModel.Component.Dispose()
at SpaghettiCode.Interface_Wyd.Dispose(Boolean disposing)
at System.ComponentModel.Component.Dispose()
at SpaghettiCode.PZ_Wydania.PZ_Wydania_Closing(Object sender, CancelEventArgs e)
at System.Windows.Forms.Form.OnClosing(CancelEventArgs e)
at System.Windows.Forms.Form._FCanClose()
at System.Windows.Forms.Form._CloseModal()
at System.Windows.Forms.Form.Close()
at SpaghettiCode.ExtendedForm.set_CloseApp(Boolean value)
at SpaghettiCode.ExtendedForm.Exit_Click(Object sender, EventArgs e)
at System.Windows.Forms.MenuItem.OnClick(EventArgs e)
at System.Windows.Forms.Menu.ProcessMnuProc(Control ctlThis, WM wm, Int32 wParam, Int32 lParam)
at System.Windows.Forms.Form.WnProc(WM wm, Int32 wParam, Int32 lParam)
at System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam)
at Microsoft.AGL.Forms.EVL.EnterModalDialog(IntPtr hwnModal)
at System.Windows.Forms.Form.ShowDialog()
at SpaghettiCode.Admin_Tools.init_tool()
at SpaghettiCode.Admin_Tools.Admin_Tools_KeyDown(Object sender, KeyEventArgs e)
at System.Windows.Forms.Control.OnKeyDown(KeyEventArgs e)
at OpenNETCF.Windows.Forms.OwnerDrawnList.OnKeyDown(KeyEventArgs e)
at System.Windows.Forms.Control.WnProc(WM wm, Int32 wParam, Int32 lParam)
at System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam)
at Microsoft.AGL.Forms.EVL.EnterModalDialog(IntPtr hwnModal)
at System.Windows.Forms.Form.ShowDialog()
at SpaghettiCode.Wybor.Alloption_menu_KeyDown(Object sender, KeyEventArgs e)
at System.Windows.Forms.Control.OnKeyDown(KeyEventArgs e)
at System.Windows.Forms.Control.WnProc(WM wm, Int32 wParam, Int32 lParam)
at System.Windows.Forms.ListView.WnProc(WM wm, Int32 wParam, Int32 lParam)
at System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam)
at Microsoft.AGL.Forms.EVL.EnterModalDialog(IntPtr hwnModal)
at System.Windows.Forms.Form.ShowDialog()
at SpaghettiCode.Form1._result_of_verification_positive(String msg)
at SpaghettiCode.Form1.send_logpass()
at SpaghettiCode.Form1.Form1_KeyDown(Object sender, KeyEventArgs e)
at System.Windows.Forms.Control.OnKeyDown(KeyEventArgs e)
at System.Windows.Forms.Control.WnProc(WM wm, Int32 wParam, Int32 lParam)
at System.Windows.Forms.ContainerControl.WnProc(WM wm, Int32 wParam, Int32 lParam)
at System.Windows.Forms.Form.WnProc(WM wm, Int32 wParam, Int32 lParam)
at System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam)
at OpenNETCF.Windows.Forms.NativeMethods.DispatchMessage(MSG& lpMsg)
at OpenNETCF.Windows.Forms.Application2.Pump()
at OpenNETCF.Windows.Forms.Application2.RunMessageLoop(Boolean showForm)
at OpenNETCF.Windows.Forms.Application2.Run(Form mainForm, Boolean runAsSingletonApp, Boolean displayMainForm)
at OpenNETCF.Windows.Forms.Application2.Run(Form mainForm)
at SpaghettiCode.Program.Main()

I am not using any special drawing in this, apart from changing column size and font which I already confirmed to not make a difference. 除了更改已确认不会改变的列大小和字体外,我没有在其中使用任何特殊的图形。

My base form class inherits from Windows.Forms, so I dispose it like this: 我的基本表单类继承自Windows.Forms,因此我将其处置为:

    public class ExtendedForm :  System.Windows.Forms.Form
    protected override void Dispose(bool disposing)
    {
        if (disposed)
            return;

        if (disposing)
        {
            if (mainTimer != null)
            {
                mainTimer.Enabled = false;
                mainTimer.Tick -= mainTimer_Tick;
                mainTimer.Dispose();
                mainTimer = null;
                isTimerDead = true;
            }
            if (panel1 != null)
            {
                panel1.Dispose();
                mainmenu.Dispose();
                Exit.Dispose();
                MenuBack.Dispose();
                panel1 = null;
                mainmenu = null;
                Exit = null;
                MenuBack = null;
            }
            if (barcode != null)
            {
                barcode.Dispose();
                barcode = null;
            }
        }
        disposed = true;
        base.Dispose(disposing);
    }

While my main interface is inherited and disposed like this: 虽然我的主界面是这样继承和处理的:

 class Interface_Wyd : ExtendedForm
 protected override void Dispose(bool disposing)
    {
        if (disposed)
            return;

        if (disposing)
        {
            // Free any other managed objects here.
            this.Timer_work.Enabled = false;
            this.Timer_work.Dispose();
            this.List_Box_.Dispose();
            //this helped me once with DisposedException in other form with datagrid. No effect here though.
            this.DataGr_.UnSelect(this.DG_intf.CurrentCell.RowNumber);

            this.DataTab_intf.Dispose();
            this.DataGr_.Dispose();

            this.LV_bott_intf.Dispose();  <--- signalizes an exception here but the problem is with DataGrid.
        }

        // Free any unmanaged objects here.
        //
        disposed = true;
        base.Dispose(true);
    }
    ~Interface_Wyd() { Dispose(false); }

For people who might struggle with the same bug ever again: Don't forget to remove controls from the main panel before disposing them. 对于可能再次遇到相同bug的人们:不要忘记在处理之前从主面板中删除控件。 I still wonder why it worked if I didn't select any of the cells in the datagrid. 我仍然想知道,如果我没有选择数据网格中的任何单元格,为什么它会起作用。 Apparently when I did, windows performed a drawing and stored handle to datagrid somewhere and tried to use it later on object that didn't exists anymore. 显然,当我这样做时,Windows执行了一个绘制并将数据网格的句柄存储在某个地方,并稍后尝试在不再存在的对象上使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM