WinForms和处置自定义控件 [英] WinForms and disposing custom controls
问题描述
我有以下类:
public class NewListBox : ListBox
{
public NewListBox()
{
}
private ImageList _myImageList;
public ImageList ImageList
{
get { return _myImageList; }
set { _myImageList = value; }
}
}
我对是否处理这个对象时会触发处置的对象领域,如在ImageList的,或者我应该实现(覆盖)的Dispose方法和做好这项工作自己?
I am interested in whether disposing of this object will trigger the disposal of fields on the object, such as the ImageList, or should i implement (override) the Dispose method and do this work myself?
推荐答案
您应该在ImageList添加到您的控件的组件集合,那么基类实现处置将处置一切的集合,你会不会有覆盖处置自己。
You should add the ImageList to your control's Components collection, then the base-class implementation of Dispose will Dispose everything in that collection, and you won't have to override Dispose yourself.
如果您是IDisposable的,但不是所有的组件成员,那么你将不得不重写处置在你的控制和你自己处置他们。
If you have any members that are IDisposable but are not Components, then you will have to override Dispose in your control and Dispose them yourself.
(我使用的严格意义上的对象,从System.ComponentModel.Component派生词组件)。
(I am using the term Component in the strict sense of objects that derive from System.ComponentModel.Component).
这篇关于WinForms和处置自定义控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!