Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically list all forms in a project

Tags:

delphi

I want to list name of all forms exist in my project in a ListBox Dynamically, then by clicking on each of them, list all buttons exist on that form in another ListBox.

But I don't know if it can be implemented and how it can.

Please help me.

Thanks

like image 849
Hamid Avatar asked Dec 03 '22 12:12

Hamid


2 Answers

In case you are on Delphi 2010 you can use RTTI to list all registered ( = somehow used in the application) form classes:

uses
  TypInfo, RTTI;

procedure ListAllFormClasses(Target: TStrings);
var
  aClass: TClass;
  context: TRttiContext;
  types: TArray<TRttiType>;
  aType: TRttiType;
begin
  context := TRttiContext.Create;
  types := context.GetTypes;
  for aType in types do begin
    if aType.TypeKind = tkClass then begin
      aClass := aType.AsInstance.MetaclassType;
      if (aClass <> TForm) and aClass.InheritsFrom(TForm) then begin
        Target.Add(aClass.ClassName);
      end;
    end;
  end;
end;

You must somehow take care that the class is not completely removed by the linker (therefor the registered hint above). Otherwise you cannot get hands on that class with the method described.

like image 60
Uwe Raabe Avatar answered Dec 22 '22 07:12

Uwe Raabe


The forms are usually listed using Screen.Forms property, ex:

procedure TForm1.Button1Click(Sender: TObject);
var
  I: Integer;

begin
  Memo1.Lines.Clear;
  for I:= 0 to Screen.CustomFormCount - 1 do
    Memo1.Lines.Add(Screen.Forms[I].Caption);
end;
like image 36
kludg Avatar answered Dec 22 '22 07:12

kludg