C# WinForms: Cannot access a dispose object

旧街凉风 提交于 2019-12-12 14:27:43

问题


I have a datagridview. This datagridview has customs datagridviewcolumns among other like datagridviewtextboxcolumns. Third-party control is hosted in each cell. So as datagridview is designed, on custom cells, content cannot be shown unless you are in cell's edit mode. So to achieve cells content to be shown I have overrided paint method in order to paint the cells when they are not in edit mode. The image to be painted in the cell is obtained in paint method and I use block like:

using (Bitmap bitmap = new Bitmap())
{
   ...
}

Also, to host a control in windows forms datagridview cells, I have a class that implements IDataGridViewEditingControl.

public class a : third-party-component, IDataGridViewEditingControl
{
}

At some point of the application, it crashes saying cannot access a disposed object. In the code I have not done any disposed on any object so I do not understand this.

From the stack it seems like the error is raised in the class above indicated but it does not indicate explicitally where.

I am sure that this is caused by the control hosted in windows forms datagridview cells. Any ideas about what can be the problem?

or maybe it is related to datagridviewtextboxcolumns? below error:

Cannot access a disposed object.
Object name: 'DataGridViewTextBoxEditingControl'.
Stack Trace:
   at System.Windows.Forms.Control.CreateHandle()
   at System.Windows.Forms.TextBoxBase.CreateHandle()
   at System.Windows.Forms.Control.get_Handle()
   at System.Windows.Forms.Control.GetSafeHandle(IWin32Window window)
   at System.Windows.Forms.ToolTip.Hide(IWin32Window win)
   at System.Windows.Forms.ToolTip.HideAllToolTips()
   at System.Windows.Forms.ToolTip.BaseFormDeactivate(Object sender, EventArgs e)
   at System.EventHandler.Invoke(Object sender, EventArgs e)
   at System.Windows.Forms.Form.OnDeactivate(EventArgs e)
   at Crownwood.DotNetMagic.Forms.WindowChrome.OnDeactivate(EventArgs e)
   at System.Windows.Forms.Form.set_Active(Boolean value)
   at System.Windows.Forms.Form.WmActivate(Message& m)
   at System.Windows.Forms.Form.WndProc(Message& m)
   at Crownwood.DotNetMagic.Forms.WindowChrome.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

回答1:


It seems you have not disposed the object created for the ToolTip. Ensure you have disposed the created object of ToolTip in the respective control's(IDataGridViewEditingControl) dispose method.

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



来源:https://stackoverflow.com/questions/13406560/c-sharp-winforms-cannot-access-a-dispose-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!