RAD Studio VCL Reference
|
Creates an instance of TMenuItem.
constructor Create(AOwner: TComponent); override;
virtual __fastcall TMenuItem(TComponent * AOwner);
Use Create to create a menu item at runtime. Menu items defined at design time using the Menu designer are created automatically.
AOwner is the component that is responsible for freeing the menu item. It becomes the value of the Owner property.
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"; // The owner will cleanup 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 cleanup these menu items. NewItem->Caption = Screen->Forms[I]->Name + "Item"; NewItem->Name = Screen->Forms[I]->Name; MainMenu1->Items->Items[1]->Add(NewItem); } }
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) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|