This is how you can find all the open forms at any given moment of time:
http://msdn.microsoft.com/en-us/library/system.windows.forms.application.openforms.aspx[
^].
You don't need to find any other forms, as only open forms are usable. If you want to examine some forms before they are shown, you need to create forms (you always create them in your code, by yourself, so you can always collect references to them) and make your own collections of the forms you want to examine.
Now, a form is a
Control
, mode exactly,
System.Windows.Forms.Control
. And all form children are also controls. The only problem is: to traverse all controls on the form, you will need to use recursion, as controls are parented hierarchically. It could be something like this:
static ProcessControls(Control parent, System.Action<Control> processingMethod) {
processingMethod(parent);
foreach(Control child in parent.Controls)
ProcessControls(child, processingMethod);
}
I don't know what do your want to do in the processing methods. Perhaps you need to process only the controls of some separate types, or only the controls which are not containers (and then you can check for
(someControl.Controls.Count == 0)
). If you need just to collect all controls satisfying certain criteria in some collection, you would need an additional parameter, for the collection. For lists, for example, it could be
System.Action<Control, System.Collections.Generic.IList<Control>>
.
You create some method processing each control with required parameter(s) to pass it as the
processingMethod
parameter (anonymous method recommended in many cases, but not required), and pass it to
ProcessControl
; the first parameter should be each form (you remember that the form is also
Control
). The method will recursively process all nested control, after the form itself.
There can be many variants of this schema.
—SA