c# - How do I get all controls of a form in Windows Forms? -
i have form named a.
a contains lots of different controls, including main groupbox. groupbox contains lots of tables , others groupboxes. want find control has e.g. tab index 9 in form a, don't know groupbox contains control.
how can this?
with recursion...
public static ienumerable<t> descendants<t>( control control ) t : class { foreach (control child in control.controls) { t childoft = child t; if (childoft != null) { yield return (t)childoft; } if (child.haschildren) { foreach (t descendant in descendants<t>(child)) { yield return descendant; } } } } you can use above function like:
var checkbox = (from c in myform.descendants<checkbox>() c.tabindex == 9 select c).firstordefault(); that first checkbox anywhere within form has tabindex of 9. can use whatever criteria want.
if aren't fan of linq query syntax, above re-written as:
var checkbox = myform.descendants<checkbox>() .firstordefault(x=>x.tabindex==9);
Comments
Post a Comment