Now, before you say it already exists (because it does) I found that those didn't really have the generic ability to find any control on a form, regardless of containers.
So, here you have a piece of code which will find any control on a form and will also search inside any containers those controls might have.
There are two ways of using this code. First is by Control Type and the second is by Control Name.
In each example below, I have used the variable "myForm" which can be any kind of Windows Form. However, you can perform the following on any control you wish. So it could have been performed on "myPanel" or "myGridBox" etc.
To use by Control Type simply perform the following:
myForm.FindControl<Label>(); This will return an IEnumerable<Label> containing every single label on your form, regardless of any container it is placed in. You will be able to iterate back up the tree using the "Control.Parent" attribute to see which container it belongs to.
Secondly, you can search by Control Name. You may also filter the Control Type:
myForm.FindControl<Control>("myLabel"); The above code will search every control on the form until it finds the control named "myLabel". "myLabel", however, is pretty obviously a Label control. So in order to speed up the comparison (not by much in 99.9% of cases, but you never know) you can do the following:
myForm.FindControl<Label>("myLabel"); And there you have it. I hope you guys can find some use out of it. Please if you do use the code snippet, I ask that you leave a comment in the top of your file.
PS. Important note - The contents will come back with no sorting. So you may find that 2 controls from your form come back, then 3 from your panel and then another 2 from your form. If you wish that level of sorting, I leave it as an exercise for the reader (however, it would be pretty easy to do in LINQ)
Please leave a quick thanks if you use the code and constructive criticism if you feel I could have done better :)