In C# (since you tagged it as such) you could use a LINQ expression like this:
List<Control> c = Controls.OfType<TextBox>().Cast<Control>().ToList();
Edit for recursion:
In this example, you first create the list of controls and then call a method to populate it. Since the method is recursive, it doesn't return the list, it just updates it.
List<Control> ControlList = new List<Control>();
private void GetAllControls(Control container)
{
foreach (Control c in container.Controls)
{
GetAllControls(c);
if (c is TextBox) ControlList.Add(c);
}
}
It may be possible to do this in one LINQ statement using the Descendants
function, though I am not as familiar with it. See this page for more information on that.
Edit 2 to return a collection:
As @ProfK suggested, a method that simply returns the desired controls is probably better practice. To illustrate this I have modified the code as follows:
private IEnumerable<Control> GetAllTextBoxControls(Control container)
{
List<Control> controlList = new List<Control>();
foreach (Control c in container.Controls)
{
controlList.AddRange(GetAllTextBoxControls(c));
if (c is TextBox)
controlList.Add(c);
}
return controlList;
}