RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
Menus.NewItem Function

Creates and initializes a menu item.

Pascal
function NewItem(const ACaption: string; AShortCut: TShortCut; AChecked: Boolean; AEnabled: Boolean; AOnClick: TNotifyEvent; hCtx: THelpContext; const AName: string): TMenuItem;
C++
TMenuItem NewItem(const AnsiString ACaption, TShortCut AShortCut, Boolean AChecked, Boolean AEnabled, TNotifyEvent AOnClick, THelpContext hCtx, const AnsiString AName);

Call NewItem to create and initialize a menu item when constructing a menu programmatically. 

The ACaption parameter specifies the string that should appear for the menu item.  

The AShortCut parameter specifies any associated shortcut.  

The AChecked parameter indicates whether the menu item appears with a check mark.  

The AEnabled parameter specifies whether the menu item is enabled or grayed. 

The AOnClick parameter specifies an event handler that executes when the user clicks the menu item. 

The hCtx parameter specifies the help context ID for the menu item. 

The AName parameter specifies the name of the menu item, which can be used to refer to the menu item in code. 

The menu item returned by NewItem can be added to a menu using the target parent's Add method. If the entire menu is being created dynamically at runtime, the new menu item can be added to an array that is passed as a parameter to the NewMenu, NewPopupMenu, or NewSubMenu function.

Warning: The item returned by NewItem does not have an owner. You are responsible for freeing its memory when it is no longer needed. The Delete and Remove methods of TMenuItem do not free memory.
 

C++ Examples: 

 

/*
This code displays a new menu named MyMenu when the user
clicks the button. MyOnExecute must be included in the
definition of the TForm1 class.  Click on the menu item
or type "Ctrl+N" to execute MyOnExecute.
*/
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  TMenuItem *Item1 = NewItem("New item", TextToShortCut("Ctrl+N"), false, true, MyOnExecute, 0, "Item1");
  Menu = NewMenu(this, "MyMenu", &Item1, 0);
}

void __fastcall TForm1::MyOnExecute(TObject *Sender)
{
  MessageDlg("Item1 has been executed.", mtInformation, TMsgDlgButtons() << mbOK, 0);
}

 

Delphi Examples: 

{
This code displays a new menu named MyMenu when the user
clicks the button. MyOnExecute must be included in the
definition of the TForm1 class.
} 
procedure TForm1.Button1Click(Sender: TObject);
begin
  Menu := NewMenu(Self, 'MyMenu',
            [NewItem('New item', TextToShortCut('Ctrl+N'),
            False, True, MyOnExecute, 0, 'Item1')]);
end;

procedure TForm1.MyOnExecute(Sender: TObject);
begin
  MessageDlg('Item1 has been executed.', mtInformation, [mbOK], 0);
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!