FormCount (C++)

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 main form's FormCreate. The other forms do not exist yet.

Code

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, will you get "Menu inserted twice"? 
                                 //Does not happen in Delphi.
  NewItem->Name = "Separator";   // The owner will clean up these menu items.
  NewItem->Caption = "-";
  // Add the new item to the Windows menu.
  MainMenu1->Items->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); // The owner will clean up these menu items.
	NewItem->Caption = Screen->Forms[I]->Name + "Item";
	NewItem->Name = Screen->Forms[I]->Name;
	MainMenu1->Items->Items[1]->Add(NewItem);
  }
}

Uses

Personal tools
Assistance