So presumably you want to get all of the controls anywhere on the form, not just top level controls. For that we'll need this handy little helper function to get all child controls, at all levels, for a particular control:
public static IEnumerable<Control> GetAllControls(Control control)
{
Stack<Control> stack = new Stack<Control>();
stack.Push(control);
while (stack.Any())
{
var next = stack.Pop();
yield return next;
foreach (Control child in next.Controls)
{
stack.Push(child);
}
}
}
(Feel free to make it an extension method if you think you'd use it enough.)
Then we can just use OfType
on that result to get the controls of a particular type:
var panels = GetAllControls(this).OfType<Panel>();