C#WinForms:无法访问一个处理对象 [英] C# WinForms: Cannot access a dispose object
问题描述
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屋!