I am dynamically creating controls on top of TabPages that are, of course, on a TabControl. I want to be able to dynamically dispose of these, too. How can I do that? Calling Clear on the TabControl's TabPages collection actually disposes the TabPages themselves, which defeats the purpose of "starting over" with new dynamically created controls on those pages.
Calling tabPageBla.Controls.Clear() is close to what I want, but it also disposes a container control that I have on each TabPage (FlowControlLayout) that I need to keep.
Is there a straightforward way to accomplish this (dispose only the dynamically-created controls, but not any of the others)?
UPDATE
Will this work - will grandchildren of TabControl1 also be found (children of the tab pages)?:
List<Control> ctrls = new List<Control>();
ctrls.AddRange(tabControl1.Controls);
foreach (var ctrl in ctrls)
{
// Controls named "panelRowBla", "richTextBoxBla", and "pictureBoxBla" need to be retained
string ctrlName = ctrl.Name;
if ((ctrlName.Contains("panelRow")) ||
(ctrlName.Contains("richTextBox")) ||
(ctrlName.Contains("pictureBox")))
{
continue;
}
}
UPDATE 2
That's odd; I could have sworn this was just compiling, but now I'm getting "Argument 1: cannot convert from 'System.Windows.Forms.Control.ControlCollection' to System.Collections.Generic.IEnumerable'"
(on the "AddRange()" line).