RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TScreen.Forms Property

Lists all the forms currently displayed in the application.

Pascal
property Forms [Index: Integer]: TForm;
C++
__property TForm * Forms[int Index];

Use Forms to access a form by index. The value of Index is a number between zero (the first form) and FormCount - 1. Forms can be used with FormCount when an application needs to iterate over all its forms, including all dialogs. 

Forms only lists the TForm descendants in the application. This does not include, for example, property pages. To get a list that includes TCustomForm descendants that do not descend from TForm, use CustomForms instead.

Warning: The order in which Forms lists its forms is affected by the Z order of the forms. Do not change the Z order of forms when using Forms to iterate over the forms in an application.
 

C++ Examples: 

 

/*
This example assumes that the main form of the application
has a main menu with a menu item. The following code adds a
separator, and the name of all forms to the menu. Do not
access FormCount in the mainform's FormCreate.  The other
forms do not exist yet!
*/
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  TMenuItem *NewItem;
  // first create the separator
  NewItem = new TMenuItem(this); // If you change the AOwner (this) to MainMenu1->Items[1] and then you add the new menu you will get "Menu inserted twice"?  Doesn't happen in Delphi.
  NewItem->Name = "Separator";
  NewItem->Caption = "-";
  // add the new item to the Windows menu
  MainMenu1->Items[1].Add(NewItem);
  // now create and add a menu item for each form
  for  (int I = 0; I < Screen->FormCount; I++)
  {
    NewItem = new TMenuItem(this);
    NewItem->Caption = Screen->Forms[I]->Name + "Item";
    NewItem->Name = Screen->Forms[I]->Name;
    MainMenu1->Items[1].Add(NewItem);
  }
}

void __fastcall TForm1::FormDestroy(TObject *Sender)
{
  // Count down, not up!
  for  (int I = MainMenu1->Items[1].Count - 1; I >= 0; I--)
  {
    delete MainMenu1->Items[1].Items[I];
  }
}

 

Delphi Examples: 

{
This example assumes that the main form of the application
has a main menu with a menu item. The following code adds a
separator, and the name of all forms to the menu. Do not
access FormCount in the mainform's FormCreate.  The other
forms do not exist yet!
}
procedure TForm1.Button1Click(Sender: TObject);
var
  NewItem: TMenuItem;
  I : integer;
begin
  { first create the separator }
  NewItem := TMenuItem.Create(MainMenu1.Items[1]);
  NewItem.Name:= 'Separator';
  NewItem.Caption := '-';
  { add the new item to the Windows menu }
  MainMenu1.Items[1].Add(NewItem);
  { now create and add a menu item for each form }
  for  I := 0 to Screen.FormCount-1 do
  begin
    NewItem := TMenuItem.Create(MainMenu1.Items[1]);
    NewItem.Caption := Screen.Forms[I].Name + 'Item';
    NewItem.Name := Screen.Forms[I].Name;
    MainMenu1.Items[1].Add(NewItem);
   end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
var
  I : Integer;
begin
  // Count down, not up!
  for  I := MainMenu1.Items[1].Count - 1 downto 0 do
  begin
    MainMenu1.Items[1].Items[I].Free;
  end;
end;

 

Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!