4

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

Konrad Viltersten
  • 32,186
  • 68
  • 222
  • 392
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
  • 27,871
  • 5
  • 44
  • 49
1

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

TorbenJ
  • 4,302
  • 11
  • 45
  • 81
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,538
  • 2
  • 31
  • 48
  • 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