I am filtering a data grid view with a TextBox in a ToolStripControlHost that is in the items of a ToolStripDropDown.
I can see the exact number of times I use the TextBox in the Visual Studio Memory Usage snapshot while debugging and they never disappear.
I have already attempted to catch all the disposing events and dispose their children as shown below but they remain in memory.
private void TxtFilter_Disposed(object sender, EventArgs e)
{
CueTextBox filter = sender as CueTextBox;
filter.KeyPress -= CmdFilter_Submit;
for (int ix = filter.Controls.Count - 1; ix >= 0; --ix)
{
// If Dispose is not called then the controls will leak forever.
filter.Controls[ix].Dispose();
}
}
How do I dispose of these correctly?
private void DisplayFilter(DataGridViewCellMouseEventArgs e)
{
int minWidth = MINIMUM_FILTER_WIDTH > Columns[e.ColumnIndex].Width ? MINIMUM_FILTER_WIDTH : Columns[e.ColumnIndex].Width;
CueTextBox TxtFilter = new CueTextBox
{
Cue = "Filter",
Size = new Size(minWidth, 30),
Tag = Columns[e.ColumnIndex].Name
};
var popup = new ToolStripDropDown
{
AutoSize = false,
Margin = Padding.Empty,
Padding = Padding.Empty
};
//popup.Closed += Popup_Closed;
ToolStripControlHost host = new ToolStripControlHost(TxtFilter)
{
Margin = Padding.Empty,
Padding = Padding.Empty,
AutoSize = false,
Size = TxtFilter.Size
};
//host.Disposed += Host_Disposed;
popup.Size = TxtFilter.Size;
popup.Items.Add(host);
popup.Show(this, GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false).X, GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false).Bottom);
TxtFilter.Focus();
}