I want to retrieve controls of a form. To do this :
internal static IEnumerable<Control> EnumereTousControle(Control controleParent)
{
foreach (Control subControl in controleParent.Controls)
{
yield return subControl;
foreach (Control c in EnumereTousControle(subControl))
yield return c;
}
}
It works fine, all children of the parent control are retrieved. But I need this method returns the parent control too. I tried this but it don’t works (because the method is recursive) :
internal static IEnumerable<Control> EnumereAllControls(Control parentControl)
{
yield return parentControl; // does not work
foreach (Control subControl in parentControl.Controls)
{
yield return subControl;
foreach (Control c in EnumereAllControls(subControl))
yield return c;
}
}
Thanks for your help !
Thnk you just had one line too many, try this: