如何处理ToolStripDropDown?
我在我的VS2012项目上运行了代码分析选项,它发现了以下内容;那些拥有一次性场如何处理ToolStripDropDown?
CA1001类型应该是 一次性对“DataGridViewColumnSelector” 实现IDisposable,因为它创建下列IDisposable的类型的成员: “CheckedListBox”,“ToolStripDropDown”。如果'DataGridViewColumnSelector' 先前已发货,则将实现IDisposable 的新成员添加到此类型被视为对现有消费者的重大更改。 DataGridColSelector DataGridViewColumnSelector.cs
我做了我的课,DataGridViewColumnSelector自IDisposable继承,我想知道要放什么东西在Dispose方法
更新:
这里是我的尝试。代码分析已经停止抱怨,因为我把课程封闭了。 我仍然不知道我是“做对”
public sealed class DataGridViewColumnSelector :IDisposable
{
private DataGridView mDataGridView = null;
private CheckedListBox mCheckedListBox;
private ToolStripDropDown mPopup;
public delegate void CustomRightClickDelegate(object sender, MouseEventArgs e);
public event CustomRightClickDelegate GridRightClickEvent;
/// <summary>
/// The max height of the popup
/// </summary>
public int MaxHeight = 300;
/// <summary>
/// The width of the popup
/// </summary>
public int Width = 200;
public DataGridView DataGridView
{
get { return this.mDataGridView; }
set
{
if (this.mDataGridView != null) this.mDataGridView.MouseDown -= this.mDataGridView_MouseDown;
this.mDataGridView = value;
if (this.mDataGridView != null) this.mDataGridView.MouseDown += this.mDataGridView_MouseDown;
}
}
void mDataGridView_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
if(this.mDataGridView.HitTest(e.X, e.Y).Type == DataGridViewHitTestType.ColumnHeader)
{
this.mCheckedListBox.Items.Clear();
foreach (DataGridViewColumn c in this.mDataGridView.Columns)
{
this.mCheckedListBox.Items.Add(c.HeaderText, c.Visible);
}
int PreferredHeight = (this.mCheckedListBox.Items.Count*20);
this.mCheckedListBox.Height = (PreferredHeight < this.MaxHeight) ? PreferredHeight : this.MaxHeight;
this.mCheckedListBox.Width = this.Width;
this.mPopup.Show(this.mDataGridView.PointToScreen(new Point(e.X, e.Y)));
}
else
{
if (this.GridRightClickEvent != null)
{
this.GridRightClickEvent.Invoke(sender, e);
}
}
}
}
public DataGridViewColumnSelector()
{
this.mCheckedListBox = new CheckedListBox();
this.mCheckedListBox.CheckOnClick = true;
this.mCheckedListBox.ItemCheck += new ItemCheckEventHandler(this.mCheckedListBox_ItemCheck);
ToolStripControlHost mControlHost = new ToolStripControlHost(this.mCheckedListBox);
mControlHost.Padding = Padding.Empty;
mControlHost.Margin = Padding.Empty;
mControlHost.AutoSize = false;
this.mPopup = new ToolStripDropDown();
this.mPopup.Padding = Padding.Empty;
this.mPopup.Items.Add(mControlHost);
}
public DataGridViewColumnSelector(DataGridView dgv)
: this()
{
this.DataGridView = dgv;
}
void mCheckedListBox_ItemCheck(object sender, ItemCheckEventArgs e)
{
this.mDataGridView.Columns[e.Index].Visible = (e.NewValue == CheckState.Checked);
}
public void Dispose()
{
//http://*.com/questions/6826958/c-toolstripdropdown-doesnt-dispose-destroyhandle
// http://msdn.microsoft.com/en-au/library/b1yfkh5e%28v=vs.71%29.aspx
// Kirsten says I dont feel sure about what I am doing here.
mCheckedListBox.Dispose();
mPopup.Dispose();
GC.SuppressFinalize(this);
}
}
你shold调用dispose那么你的形式处置。
另外。你的IDisposable
的实现缺少一些重要的事情。
1)您应该确保没有事件订阅您的自定义事件。这可能最终会给你的应用程序一个内存泄漏。
//in dispose
GridRightClickEvent = null
2)MSDN有implemeting IDisposable
public sealed class DataGridViewColumnSelector : IDisposable
{
//removed: ~DataGridViewColumnSelector (){ Dispose(false); /*destructor*/ }
//class context omitted
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if(disposing)
{
//kill the reference - do not dispose the object.
//it was *not* created here, os it should *not* be disposed here
mDataGridView = null;
//makes sure no outside object has a reference
//to the event - thus keeping it alive when it should be garbagecollected
GridRightClickEvent = null;
if(mCheckedListBox != null) mCheckedListBox.Dispose();
if(mPopup != null) mPopup.Dispose();
if(mControlHost != null) mControlHost .Dispose();
}
}
}
实现终结器(aka析构函数)不是最佳实践。只有在类存储非托管资源时才需要这样做,这样就不会像这样的代码那样。产生这个错误的最强烈的指示是当Dispose(bool)方法在* disposing *为false时没有做任何有用的事情。就像在你的片段中一样。 – 2013-04-22 10:50:53
@HansPassant这使得sence。 我编辑了代码exampel - 删除析构函数 – 2013-04-22 11:12:10
那么下面//在这里处理你的配置? 是不是 'GridRightClickEvent = null; mCheckedListBox.Dispose(); mPopup.Dispose();' – 2013-04-22 15:55:47
这取决于你如何实现'IDisposable',以及如何往往你创建一个新的实例的最佳做法。试着展示你的代码,以及你如何使用它。 – 2013-04-22 05:53:13
你正在处理你的方法中的正确控制。我建议你在处理一个控件之前做一个空检查,然后将它设置为null(只是为了确保GC确实认识到你不再需要这个对象)。如果你不需要它,不要GC会自动调用GC方法。 – wonko79 2013-04-22 07:02:58