내 VS2012 프로젝트에서 코드 분석 옵션을 실행했으며 다음을 발견했습니다. 'CheckedListBox', '으로 ToolStripDropDown을'일회용 필드를 소유ToolStripDropDown을 어떻게 처리합니까?
CA1001 유형은 다음으로 IDisposable 유형의 구성원을 만들기 때문에 일회용 가 'DataGridViewColumnSelector' 에는 IDisposable을 구현해야합니다. 'DataGridViewColumnSelector' 이 이전에 배송 된 경우 IDisposable 을 구현하는 새 멤버를이 유형에 추가하면 기존 소비자의 주요 변경 사항으로 간주됩니다. DataGridColSelector DataGridViewColumnSelector.cs
나는 내 수업을 만들어, DataGridViewColumnSelector이는 IDisposable에서 상속 및 처분 방법에 넣어 무엇인지 궁금
업데이트 :
가 여기 내 시도이다. 수업을 봉쇄 한 이후로 코드 분석이 중단되었습니다. 나는 여전히
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://stackoverflow.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);
}
}
, 얼마나 자주 새 인스턴스를 만듭니다 위치 : implemeting위한 가장 좋은 방법이있다. 코드를 표시하고 사용 방법을 시험해보십시오. –
메서드에 올바른 컨트롤을 배치하고 있습니다. 난 당신이 컨트롤을 처분하기 전에 널 체크를 제안하고 나중에 null로 설정 (그냥 GC가 정말로 당신이 더 이상이 개체가 필요하지 않습니다 인식하고 있는지 확인). 당신이 정말로 필요하지 않으면, GC 메서드를 호출하면 GC가 자동으로 작동합니다. – wonko79