4

I can get all UI controls on a Form but how to find controls on a certain UserControl?

Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
Fedor
  • 43
  • 1
  • 3

3 Answers3

5

You can use Linq operator OfType and Controls property

var controls = YourForm
               .YourUserControl
               .Controls.OfType<TextBox>();
foreach(var control in controls)
{
    ....
}

Link : http://msdn.microsoft.com/fr-fr/library/vstudio/bb360913.aspx

Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51
1

You may have a look at the FindName(string) method of UserControl

TorbenJ
  • 4,462
  • 11
  • 47
  • 84
1

If you only want to look in the immediate object you can do FindName:

object foundControl = someParentControl.FindName("nameOfChild");

or if you want recursive ways then you can look at this post here: How can I find WPF controls by name or type?

Community
  • 1
  • 1
Bill Tarbell
  • 4,933
  • 2
  • 32
  • 52
  • This is no good if you want *all* the controls and you don't care or know what their names are. – Ben Sep 09 '17 at 16:37