FormCount (C++)
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
- Vcl.Menus.TMenu.Items ( fr | de | ja )
- Vcl.Forms.TScreen.FormCount ( fr | de | ja )
- Vcl.Forms.TScreen.Forms ( fr | de | ja )
- Vcl.Menus.TMenuItem.Add ( fr | de | ja )
- Vcl.Menus.TMenuItem.Create ( fr | de | ja )
- Vcl.Menus.TMenuItem.Caption ( fr | de | ja )