RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TMenuItem.Insert Method

Inserts a menu item into a specified position in the Items array.

Pascal
procedure Insert(Index: Integer; Item: TMenuItem);
C++
__fastcall Insert(int Index, TMenuItem Item);

Use Insert to insert a new menu item to the dropdown menu for this menu item. If this menu item is the value of the Items property of a main menu or pop-up menu, Insert inserts an item into that menu. Specify the new menu item to add as the value of the Item parameter, and the position in the Items array as the value of the Index parameter.  

Delphi Examples: 

 

{
This code inserts a menu item after the first item in a menu
named FileMenu:
} 
procedure TForm1.InsertMenuItemClick(Sender: TObject);
var
  NewItem: TMenuItem;
begin
  NewItem := TMenuItem.Create(FileMenu);
  try
    NewItem.Caption := 'Do this';
    FileMenu.Insert(1, NewItem);
  except
    NewItem.Free; // Throw away the menu item if there is an error.
    raise; // reraise the exception
  end;
  // Don't free the menu item on success.  Need it for the menu!
end;

// Free all the menu items that we have created dynamically.
procedure TForm1.FormDestroy(Sender: TObject);
var
  I : Integer;
begin
  for I := FileMenu.Count - 1 downto 0 do  // Index backwards because Count changes as you remove items.
    if FileMenu.Items[I].Name = '' then  // Items created dynamically have not name.
      FileMenu.Items[I].Free;
end;

 

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