You will need to use recursion. The following is a C# solution using an extension method and goes a little beyond the scope of your question but I just pulled it from our framework.
static partial class ControlExtensions
{
public static void ApplyToMatchingChild(this Control parent, Action<Control> actionToApplyWhenFound, bool keepApplyingForever, params Func<Control, bool>[] matchingChildCriteria)
{
ControlEventHandler reapplyEventHandler = null;
if (keepApplyingForever)
{
reapplyEventHandler = (s, e) =>
{
ApplyToMatchingChild(e.Control, actionToApplyWhenFound, keepApplyingForever, matchingChildCriteria);
};
}
SearchForMatchingChildTypes(parent, actionToApplyWhenFound, reapplyEventHandler, matchingChildCriteria);
}
private static void SearchForMatchingChildTypes(Control control, Action<Control> actionToApplyWhenFound, ControlEventHandler reapplyEventHandler, params Func<Control, bool>[] matchingChildCriteria)
{
if (matchingChildCriteria.Any(criteria => criteria(control)))
{
actionToApplyWhenFound(control);
}
if (reapplyEventHandler != null)
{
control.ControlAdded += reapplyEventHandler;
}
if (control.HasChildren)
{
foreach (var ctl in control.Controls.Cast<Control>())
{
SearchForMatchingChildTypes(ctl, actionToApplyWhenFound, reapplyEventHandler, matchingChildCriteria);
}
}
}
}
And to call:
myControl.ApplyToMatchingChild(c => { /* Do Stuff to c */ return; }, false, c => c is TextBox);
That will apply a function to all child textboxes. You can use the keepApplyingForever
parameter to ensure that your function will be applied when child controls are later added. The function will also allow you to specify any number of matching criteria, for example, if the control is also a label or some other criteria.
We actually use this as a neat way to call our dependency injection container for every UserControl added to our Form.
I'm sure you shouldn't have much issue converting it to VB.NET too... Also, If you don't want the "keepApplyingForever" functionality, it should be easy enough to strip that out too.