Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <pre><code>static partial class ControlExtensions { public static void ApplyToMatchingChild(this Control parent, Action&lt;Control&gt; actionToApplyWhenFound, bool keepApplyingForever, params Func&lt;Control, bool&gt;[] matchingChildCriteria) { ControlEventHandler reapplyEventHandler = null; if (keepApplyingForever) { reapplyEventHandler = (s, e) =&gt; { ApplyToMatchingChild(e.Control, actionToApplyWhenFound, keepApplyingForever, matchingChildCriteria); }; } SearchForMatchingChildTypes(parent, actionToApplyWhenFound, reapplyEventHandler, matchingChildCriteria); } private static void SearchForMatchingChildTypes(Control control, Action&lt;Control&gt; actionToApplyWhenFound, ControlEventHandler reapplyEventHandler, params Func&lt;Control, bool&gt;[] matchingChildCriteria) { if (matchingChildCriteria.Any(criteria =&gt; criteria(control))) { actionToApplyWhenFound(control); } if (reapplyEventHandler != null) { control.ControlAdded += reapplyEventHandler; } if (control.HasChildren) { foreach (var ctl in control.Controls.Cast&lt;Control&gt;()) { SearchForMatchingChildTypes(ctl, actionToApplyWhenFound, reapplyEventHandler, matchingChildCriteria); } } } } </code></pre> <p>And to call:</p> <pre><code>myControl.ApplyToMatchingChild(c =&gt; { /* Do Stuff to c */ return; }, false, c =&gt; c is TextBox); </code></pre> <p>That will apply a function to all child textboxes. You can use the <code>keepApplyingForever</code> 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.</p> <p>We actually use this as a neat way to call our dependency injection container for every UserControl added to our Form.</p> <p>I'm sure you shouldn't have much issue <a href="http://www.developerfusion.com/tools/convert/csharp-to-vb/" rel="nofollow">converting it to VB.NET</a> too... Also, If you don't want the "keepApplyingForever" functionality, it should be easy enough to strip that out too.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload