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.

有帮助吗?

解决方案

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;

其他提示

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]);

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top