FormCount (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

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.

Code

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;

Uses

Personal tools
Assistance