C#WinForms:无法访问一个处理对象 [英] C# WinForms: Cannot access a dispose object

查看:179
本文介绍了C#WinForms:无法访问一个处理对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datagridview。此datagridview包含海报datagridviewcolumns等datagridviewtextbox列。第三方控件托管在每个单元格中。因为datagridview是设计在自定义单元格上,除非您处于单元格的编辑模式,否则不能显示内容。所以为了实现细胞内容的显示,我有一个超绘画的方法,以便在不处于编辑模式时绘制单元格。要在单元格中绘制的图像在绘制方法中获得,我使用块:

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

另外,在windows窗体中托管一个控件datagridview单元格,我有一个类实现IDataGridViewEditingControl。

  public class a:third party - 组件,IDataGridViewEditingControl 
{
}

在应用程序的某个时间点,它崩溃说不能访问被处置的对象。
在代码中我没有对任何对象进行任何处理,所以我不明白这一点。



从堆栈看来,上面提到的类,但它并没有明确指出哪里。



我确信这是由Windows窗体datagridview单元格中托管的控件引起的。关于什么可能是问题的任何想法?



或者可能与datagridviewtextboxcolumns有关?以下错误:

 无法访问已处理的对象。 
对象名:'DataGridViewTextBoxEditingControl'。
堆栈跟踪:
在System.Windows.Forms.Control.CreateHandle()
在System.Windows.Forms.TextBoxBase.CreateHandle()
在System.Windows.Forms。 Control.get_Handle()
在System.Windows.Forms.Control.GetSafeHandle(IWin32Window窗口)
在System.Windows.Forms.ToolTip.Hide(IWin32Window win)
在System.Windows。 Form.ToolTip.HideAllToolTips()
在System.Windows.Forms.ToolTip.BaseFormDeactivate(对象发件人,EventArgs e)
在System.EventHandler.Invoke(对象发件人,EventArgs e)
在System.Windows.Forms.Form.OnDeactivate(EventArgs e)
在Crownwood.DotNetMagic.Forms.WindowChrome.OnDeactivate(EventArgs e)
在System.Windows.Forms.Form.set_Active(布尔值)
在System.Windows.Forms.Form.WmActivate(Message& m)
在System.Windows.Forms.Form.WndProc(Message& m)
在Crownwood.DotNetMagic.Forms.WindowChrome。 WndProc(Message& m)
在System.Windows.Forms.Control.ControlNativeWindow.O nMessage(Message& m)
在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)


解决方案

似乎你没有处理为工具提示确保您已将ToolTip的创建对象放在相应的控件(IDataGridViewEditingControl)处理方法中。



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


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)

解决方案

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); }

这篇关于C#WinForms:无法访问一个处理对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆