I'm trying to loop through the controls of a Windows Form by using something like:
public void FindTheControls(List<Control> foundSofar, Control parent)
{
foreach(var c in parent.Controls)
{
if (c is IControl) //Or whatever that is you checking for
{
foundSofar.Add(c);
if(c.Controls.Count > 0)
{
this.FindTheControls(foundSofar, c);
}
}
}
}
Then I want to store the names of the found controls in the Form in the List foundSofar
:
private void button1_Click(object sender, EventArgs e)
{
List<Control> foundSofar = new List<Control>();
Form c = new Form();
FindTheControls(foundSofar, c.Controls);
}
However c.Controls
doesn't get recognised by the compiler, and so I can't get the controls included in Form.