-5

I am working on an application for Windows and OSX and I'd like to find all the subforms of the main TForm object in the Application.

Is there a way to do this in Delphi XE5? Simply, I'd like to iterate all components of the application.

Moved to the question from the comments:

I'm looking to find every TForm descendant in Application.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490

3 Answers3

3

Do you mean any form created via the application's main form in a manner like this?

procedure TMyMainForm.CreateSubForm;
begin
   TMySubForm.Create(Self);
end;

Try this

procedure FindMainFormSubForms(list : TList<TForm>);
var
    i : integer;
    mainForm : TForm;
begin
    mainForm := Application.MainForm;
    for i := 0 to mainForm.ComponentCount - 1 do
    begin
        if mainForm.Components[i] is TForm then
            list.Add(TForm(mainForm.Components[i]));
    end;
end;
JamesT
  • 2,988
  • 23
  • 29
  • 1
    A similar approach can be used if the `MainForm` is the `Parent` of the sub TForm objects, by iterating the `Controls[]` list instead of the `Components[]` list. – Remy Lebeau Feb 19 '14 at 20:27
  • And what about forms that are created in run-time... and assigned to the global variable in project main Unit.pas file? Are they also on Components[] list? – Sebastian Xawery Wiśniowiecki Feb 20 '14 at 07:18
  • What if the form is created without an owner? I do that habitually. – David Heffernan Feb 20 '14 at 08:18
  • If it hasn't got the main form as either its owner or parent I'm not sure you could call it a "sub-form" of the main form. At that point you'd have to maintain your own list on the main form, in which case you'd already have a reference to them and there's no question to answer. My answer does state it's assumptions up front and it's pretty poorly worded question – JamesT Feb 20 '14 at 08:54
  • @JTolley From the comments to the question: "find every TForm descendant in Application" – David Heffernan Feb 20 '14 at 10:34
  • That's a valid response but my answer was before that comment and I haven't edited it to reflect the clarification. That may mean my answer is not the one that should be accepted. I'm entirely fine with that. – JamesT Feb 20 '14 at 10:51
  • It's cool. The question was very vague. And still is somewhat unclear. – David Heffernan Feb 20 '14 at 11:08
3

In FMX you use the TScreen.Forms[] property to enumerate all form objects in the application:

for i := 0 to Screen.FormCount-1 do
  DoSomethingWith(Screen.Forms[i]);
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
2

All forms of the total application reside in Screen.CustomForms. You could iterate over them using Screen.CustomFormCount.

Filter options:

  • If you only want forms that are owned by the main form, check for Screen.CustomForms[I].Owner = Application.MainForm;
  • If you only want forms that are parented by the main form, check for Screen.CustomForms[I].Parent = Application.MainForm;

MDI:

If your main form is an MDI Form, and you would like to know all its MDI child forms, then all child forms reside in (Application.)MainForm.MDIChildren, which you could iterate over by using (Application.)MainForm.MDIChildCount.

Community
  • 1
  • 1
NGLN
  • 43,011
  • 8
  • 105
  • 200