Menu.MenuItemCollection.Add Método

Definição

Adiciona um novo MenuItem à coleção.Adds a new MenuItem to the collection.

Sobrecargas

Add(String)

Adiciona um novo MenuItem, até o final do menu atual, com uma legenda especificada.Adds a new MenuItem, to the end of the current menu, with a specified caption.

Add(MenuItem)

Adiciona um MenuItem criado anteriormente ao final do menu atual.Adds a previously created MenuItem to the end of the current menu.

Add(Int32, MenuItem)

Adiciona um MenuItem criado anteriormente no índice especificado na coleção de itens de menu.Adds a previously created MenuItem at the specified index within the menu item collection.

Add(String, EventHandler)

Adiciona um novo MenuItem no final do menu atual com uma legenda especificada e um manipulador de eventos especificado para o evento Click.Adds a new MenuItem to the end of the current menu with a specified caption and a specified event handler for the Click event.

Add(String, MenuItem[])

Adiciona um novo MenuItem ao final nesse menu com a legenda, manipulador de eventos Click e itens especificados.Adds a new MenuItem to the end of this menu with the specified caption, Click event handler, and items.

Add(String)

Adiciona um novo MenuItem, até o final do menu atual, com uma legenda especificada.Adds a new MenuItem, to the end of the current menu, with a specified caption.

public:
 virtual System::Windows::Forms::MenuItem ^ Add(System::String ^ caption);
public virtual System.Windows.Forms.MenuItem Add (string caption);
abstract member Add : string -> System.Windows.Forms.MenuItem
override this.Add : string -> System.Windows.Forms.MenuItem
Public Overridable Function Add (caption As String) As MenuItem

Parâmetros

caption
String

A legenda do item de menu.The caption of the menu item.

Retornos

MenuItem

Um MenuItem que representa o item de menu sendo adicionado à coleção.A MenuItem that represents the menu item being added to the collection.

Exemplos

O exemplo de código a seguir usa a classe derivada MainMenu para criar um menu principal, mainMenu1 , que tem dois MenuItem objetos adicionados à sua MenuItems coleção.The following code example uses the derived class MainMenu to create a main menu, mainMenu1, that has two MenuItem objects added to its MenuItems collection. Em seguida, o código é atribuído mainMenu1 à Menu Propriedade do Form .The code then assigns mainMenu1 to the Menu property of the Form. Este exemplo requer que o código definido neste exemplo esteja localizado com em um formulário.This example requires that the code defined in this example is located with in a form.

private:
   void InitializeMyMainMenu()
   {
      // Create the MainMenu.
      MainMenu^ mainMenu1 = gcnew MainMenu;
      
      /* Use the MenuItems property to call the Add method
         to add two new MenuItem objects to the MainMenu. */
      mainMenu1->MenuItems->Add( "&File" );
      mainMenu1->MenuItems->Add( "&Edit" );
      
      // Assign mainMenu1 to the form.
      this->Menu = mainMenu1;
   }
private void InitializeMyMainMenu()
{
   // Create the MainMenu.
   MainMenu mainMenu1 = new MainMenu();
   
   /* Use the MenuItems property to call the Add method
      to add two new MenuItem objects to the MainMenu. */
   mainMenu1.MenuItems.Add ("&File");
   mainMenu1.MenuItems.Add ("&Edit");

   // Assign mainMenu1 to the form.
   this.Menu = mainMenu1;
}
   
Private Sub InitializeMyMainMenu()
    ' Create the MainMenu.
    Dim mainMenu1 As New MainMenu()
       
    ' Use the MenuItems property to call the Add method
    ' to add two new MenuItem objects to the MainMenu. 
    mainMenu1.MenuItems.Add("&File")
    mainMenu1.MenuItems.Add("&Edit")
       
    ' Assign mainMenu1 to the form.
    Me.Menu = mainMenu1
End Sub

Comentários

Um MenuItem só pode estar contido em um menu por vez e não pode ser adicionado mais de uma vez ao mesmo menu.A MenuItem can only be contained in one menu at a time, and cannot be added more than once to the same menu. Para reutilizar um MenuItem em mais de um menu, use o CloneMenu método da MenuItem classe.To reuse a MenuItem in more than one menu, use the CloneMenu method of the MenuItem class. Para remover um MenuItem que você adicionou anteriormente, use o Remove método.To remove a MenuItem that you have previously added, use the Remove method.

Confira também

Aplica-se a

Add(MenuItem)

Adiciona um MenuItem criado anteriormente ao final do menu atual.Adds a previously created MenuItem to the end of the current menu.

public:
 virtual int Add(System::Windows::Forms::MenuItem ^ item);
public virtual int Add (System.Windows.Forms.MenuItem item);
abstract member Add : System.Windows.Forms.MenuItem -> int
override this.Add : System.Windows.Forms.MenuItem -> int
Public Overridable Function Add (item As MenuItem) As Integer

Parâmetros

item
MenuItem

O MenuItem a ser adicionado.The MenuItem to add.

Retornos

Int32

O índice de base zero no qual o item está armazenado na coleção.The zero-based index where the item is stored in the collection.

Exemplos

O exemplo de código a seguir cria uma instância da classe derivada, MainMenu e adiciona uma MenuItem à sua coleção de MenuItem objetos.The following code example creates an instance of the derived class, MainMenu, and adds a MenuItem to its collection of MenuItem objects. Este exemplo requer que o método definido neste exemplo esteja localizado dentro da classe de um formulário e chamado por um método nessa classe de formulário.This example requires that the method defined in this example is located within the class for a form and called by a method in that form class.

private:
   void InitializeMyMainMenu()
   {
      // Create the MainMenu and the MenuItem to add.
      MainMenu^ mainMenu1 = gcnew MainMenu;
      MenuItem^ menuItem1 = gcnew MenuItem( "&File" );
      
      /* Use the MenuItems property to call the Add method
         to add the MenuItem to the MainMenu menu item collection. */
      mainMenu1->MenuItems->Add( menuItem1 );
      
      // Assign mainMenu1 to the form.
      this->Menu = mainMenu1;
   }
private void InitializeMyMainMenu()
{
   // Create the MainMenu and the MenuItem to add.
   MainMenu mainMenu1 = new MainMenu();
   MenuItem menuItem1 = new MenuItem("&File");
   
   /* Use the MenuItems property to call the Add method
      to add the MenuItem to the MainMenu menu item collection. */
   mainMenu1.MenuItems.Add (menuItem1);

   // Assign mainMenu1 to the form.
   this.Menu = mainMenu1;
}

Private Sub InitializeMyMainMenu()
    ' Create the MainMenu and the MenuItem to add.
    Dim mainMenu1 As New MainMenu()
    Dim menuItem1 As New MenuItem("&File")
       
    ' Use the MenuItems property to call the Add method
    ' to add the MenuItem to the MainMenu menu item collection. 
    mainMenu1.MenuItems.Add(menuItem1)
       
    ' Assign mainMenu1 to the form.
    Me.Menu = mainMenu1
End Sub

Comentários

Um MenuItem só pode estar contido em um menu por vez e não pode ser adicionado mais de uma vez ao mesmo menu.A MenuItem can only be contained in one menu at a time, and cannot be added more than once to the same menu. Para reutilizar um MenuItem em mais de um menu, use o CloneMenu método da MenuItem classe.To reuse a MenuItem in more than one menu, use the CloneMenu method of the MenuItem class. Para remover um MenuItem que você adicionou anteriormente, use o Remove método.To remove a MenuItem that you have previously added, use the Remove method.

Esta versão do Add método permite adicionar objetos criados anteriormente MenuItem ao final da coleção de itens de menu.This version of the Add method allows you to add previously created MenuItem objects to the end of the menu item collection.

Confira também

Aplica-se a

Add(Int32, MenuItem)

Adiciona um MenuItem criado anteriormente no índice especificado na coleção de itens de menu.Adds a previously created MenuItem at the specified index within the menu item collection.

public:
 virtual int Add(int index, System::Windows::Forms::MenuItem ^ item);
public virtual int Add (int index, System.Windows.Forms.MenuItem item);
abstract member Add : int * System.Windows.Forms.MenuItem -> int
override this.Add : int * System.Windows.Forms.MenuItem -> int
Public Overridable Function Add (index As Integer, item As MenuItem) As Integer

Parâmetros

index
Int32

A posição na qual adicionar o novo item.The position to add the new item.

item
MenuItem

O MenuItem a ser adicionado.The MenuItem to add.

Retornos

Int32

O índice de base zero no qual o item está armazenado na coleção.The zero-based index where the item is stored in the collection.

Exceções

O MenuItem que está sendo adicionado já está em uso.The MenuItem being added is already in use.

O índice fornecido no parâmetro index é maior que o tamanho da coleção.The index supplied in the index parameter is larger than the size of the collection.

Exemplos

O exemplo de código a seguir cria uma instância da classe derivada, MainMenu e adiciona um MenuItem objeto à sua coleção de MenuItem objetos em um local específico na coleção de itens de menu.The following code example creates an instance of the derived class, MainMenu, and adds a MenuItem object to its collection of MenuItem objects at a specific location in the menu item collection. Este exemplo requer que o método definido neste exemplo esteja localizado dentro da classe de um formulário e chamado por um método nessa classe de formulário.This example requires that the method defined in this example is located within the class for a form and called by a method in that form class.

private:
   void InitializeMyMainMenu()
   {
      // Create the MainMenu and the MenuItem to add.
      MainMenu^ mainMenu1 = gcnew MainMenu;
      MenuItem^ menuItem1 = gcnew MenuItem( "&File" );
      
      /* Use the MenuItems property to call the Add method
         to add the MenuItem to mainMenu1 at specific index. */
      mainMenu1->MenuItems->Add( 0, menuItem1 );
      
      // Assign mainMenu1 to the form.
      this->Menu = mainMenu1;
   }
private void InitializeMyMainMenu()
{
   // Create the MainMenu and the MenuItem to add.
   MainMenu mainMenu1 = new MainMenu();
   MenuItem menuItem1 = new MenuItem("&File");
   
   /* Use the MenuItems property to call the Add method
      to add the MenuItem to mainMenu1 at specific index. */
   mainMenu1.MenuItems.Add (0, menuItem1);

   // Assign mainMenu1 to the form.
   this.Menu = mainMenu1;
}

Private Sub InitializeMyMainMenu()
    ' Create the MainMenu and the MenuItem to add.
    Dim mainMenu1 As New MainMenu()
    Dim menuItem1 As New MenuItem("&File")
       
    ' Use the MenuItems property to call the Add method
    ' to add the MenuItem to mainMenu1 at specific index. 
    mainMenu1.MenuItems.Add(0, menuItem1)
       
    ' Assign mainMenu1 to the form.
    Me.Menu = mainMenu1
End Sub

Comentários

Um MenuItem só pode estar contido em um menu por vez e não pode ser adicionado mais de uma vez ao mesmo menu.A MenuItem can only be contained in one menu at a time, and cannot be added more than once to the same menu. Para reutilizar um MenuItem em mais de um menu, use o CloneMenu método da MenuItem classe.To reuse a MenuItem in more than one menu, use the CloneMenu method of the MenuItem class. Para remover um MenuItem que você adicionou anteriormente, use o Remove método.To remove a MenuItem that you have previously added, use the Remove method.

Esta versão do Add método permite adicionar objetos criados anteriormente MenuItem a um local de índice específico dentro da coleção.This version of the Add method allows you to add previously created MenuItem objects to a specific index location within the collection. Qualquer um MenuItem atualmente localizado no índice e todos os MenuItem objetos após esse índice são movidos para o próximo índice mais baixo na coleção.Any MenuItem currently located at that index, and all MenuItem objects after that index, are moved to the next lowest index in the collection.

Confira também

Aplica-se a

Add(String, EventHandler)

Adiciona um novo MenuItem no final do menu atual com uma legenda especificada e um manipulador de eventos especificado para o evento Click.Adds a new MenuItem to the end of the current menu with a specified caption and a specified event handler for the Click event.

public:
 virtual System::Windows::Forms::MenuItem ^ Add(System::String ^ caption, EventHandler ^ onClick);
public virtual System.Windows.Forms.MenuItem Add (string caption, EventHandler onClick);
abstract member Add : string * EventHandler -> System.Windows.Forms.MenuItem
override this.Add : string * EventHandler -> System.Windows.Forms.MenuItem
Public Overridable Function Add (caption As String, onClick As EventHandler) As MenuItem

Parâmetros

caption
String

A legenda do item de menu.The caption of the menu item.

onClick
EventHandler

Um EventHandler que representa o manipulador de eventos que é chamado quando o item é clicado pelo usuário ou então quando um usuário pressiona uma tecla de atalho do item de menu.An EventHandler that represents the event handler that is called when the item is clicked by the user, or when a user presses an accelerator or shortcut key for the menu item.

Retornos

MenuItem

Um MenuItem que representa o item de menu sendo adicionado à coleção.A MenuItem that represents the menu item being added to the collection.

Exemplos

O exemplo de código a seguir usa a classe derivada MainMenu para criar um menu principal, mainMenu1 , que tem dois MenuItem objetos adicionados à sua MenuItems coleção.The following code example uses the derived class MainMenu to create a main menu, mainMenu1, that has two MenuItem objects added to its MenuItems collection. O código usa essa versão do Add método para definir um manipulador de eventos para o Click evento do segundo item de menu adicionado à coleção.The code uses this version of the Add method to define an event handler for the Click event of the second menu item added to the collection. Em seguida, o código é atribuído mainMenu1 à Menu Propriedade do Form .The code then assigns mainMenu1 to the Menu property of the Form. Este exemplo requer que o código definido neste exemplo esteja localizado em um formulário.This example requires that the code defined in this example is located within a form.

private:
   void InitializeMyMainMenu()
   {
      // Create the MainMenu.
      MainMenu^ mainMenu1 = gcnew MainMenu;
      
      /* Use the MenuItems property to call the Add method
         to add two new MenuItem objects to the MainMenu. */
      mainMenu1->MenuItems->Add( "&File" );
      mainMenu1->MenuItems->Add( "&Edit", gcnew EventHandler(
         this, &Form1::menuItem2_Click ) );
      
      // Assign mainMenu1 to the form.
      this->Menu = mainMenu1;
   }

private:
   void menuItem2_Click( System::Object^ sender, System::EventArgs^ e )
   {
      // Insert code to handle Click event.
   }
private void InitializeMyMainMenu()
{
   // Create the MainMenu.
   MainMenu mainMenu1 = new MainMenu();
   
   /* Use the MenuItems property to call the Add method
      to add two new MenuItem objects to the MainMenu. */
   mainMenu1.MenuItems.Add ("&File");
   mainMenu1.MenuItems.Add ("&Edit", new EventHandler (menuItem2_Click));

   // Assign mainMenu1 to the form.
   this.Menu = mainMenu1;
}

private void menuItem2_Click(System.Object sender, System.EventArgs e)
{
   // Insert code to handle Click event.
}

Private Sub InitializeMyMainMenu()
    ' Create the MainMenu.
    Dim mainMenu1 As New MainMenu()
       
    ' Use the MenuItems property to call the Add method
    ' to add two new MenuItem objects to the MainMenu. 
    mainMenu1.MenuItems.Add("&File")
    mainMenu1.MenuItems.Add("&Edit", _
       New EventHandler(AddressOf menuItem2_Click))
       
    ' Assign mainMenu1 to the form.
    Me.Menu = mainMenu1
End Sub    
   
Private Sub menuItem2_Click(sender As System.Object, e As System.EventArgs)
    ' Insert code to handle Click event.
End Sub

Comentários

Um MenuItem só pode estar contido em um menu por vez e não pode ser adicionado mais de uma vez ao mesmo menu.A MenuItem can only be contained in one menu at a time, and cannot be added more than once to the same menu. Para reutilizar um MenuItem em mais de um menu, use o CloneMenu método da MenuItem classe.To reuse a MenuItem in more than one menu, use the CloneMenu method of the MenuItem class. Para remover um MenuItem que você adicionou anteriormente, use o Remove método.To remove a MenuItem that you have previously added, use the Remove method.

Esta versão do Add método permite que você especifique uma legenda para o item de menu e um delegado para manipular o Click evento.This version of the Add method allows you to specify a caption for the menu item and a delegate to handle the Click event. Você pode usar esta versão do Add método se seu aplicativo já tiver um manipulador de eventos para manipular o Click evento.You can use this version of the Add method if your application already has an event handler to handle the Click event.

Observação

O Click evento não é gerado para um MenuItem que contém itens de submenu.The Click event is not raised for a MenuItem that contains submenu items.

Confira também

Aplica-se a

Add(String, MenuItem[])

Adiciona um novo MenuItem ao final nesse menu com a legenda, manipulador de eventos Click e itens especificados.Adds a new MenuItem to the end of this menu with the specified caption, Click event handler, and items.

public:
 virtual System::Windows::Forms::MenuItem ^ Add(System::String ^ caption, cli::array <System::Windows::Forms::MenuItem ^> ^ items);
public virtual System.Windows.Forms.MenuItem Add (string caption, System.Windows.Forms.MenuItem[] items);
abstract member Add : string * System.Windows.Forms.MenuItem[] -> System.Windows.Forms.MenuItem
override this.Add : string * System.Windows.Forms.MenuItem[] -> System.Windows.Forms.MenuItem
Public Overridable Function Add (caption As String, items As MenuItem()) As MenuItem

Parâmetros

caption
String

A legenda do item de menu.The caption of the menu item.

items
MenuItem[]

Uma matriz de objetos MenuItem que esta MenuItem conterá.An array of MenuItem objects that this MenuItem will contain.

Retornos

MenuItem

Um MenuItem que representa o item de menu sendo adicionado à coleção.A MenuItem that represents the menu item being added to the collection.

Comentários

Um MenuItem só pode estar contido em um menu por vez e não pode ser adicionado mais de uma vez ao mesmo menu.A MenuItem can only be contained in one menu at a time, and cannot be added more than once to the same menu. Para reutilizar um MenuItem em mais de um menu, use o CloneMenu método da MenuItem classe.To reuse a MenuItem in more than one menu, use the CloneMenu method of the MenuItem class. Para remover um MenuItem que você adicionou anteriormente, use o Remove método.To remove a MenuItem that you have previously added, use the Remove method.

Esta versão do Add método permite que você especifique uma legenda para o item de menu e um delegado que manipulará seu Click evento.This version of the Add method allows you to specify a caption for the menu item and a delegate that will handle its Click event. Você pode usar esta versão do Add método se seu aplicativo já tiver um manipulador de eventos para manipular o Click evento.You can use this version of the Add method if your application already has an event handler to handle the Click event. Esta versão do método também permite que você especifique uma matriz de objetos criados anteriormente MenuItem que você deseja adicionar à coleção.This version of the method also allows you to specify an array of previously created MenuItem objects that you want to add to the collection. Você pode usar esse recurso para reutilizar MenuItem objetos existentes que foram clonados usando o CloneMenu método.You can use this feature to reuse existing MenuItem objects that have been cloned using the CloneMenu method. Se o items parâmetro não estiver vazio ou null o MenuItem que está sendo adicionado à coleção conterá itens de submenu.If the items parameter is not empty or null, the MenuItem being added to the collection will contain submenu items.

Observação

O Click evento não é gerado para um MenuItem que contém itens de submenu.The Click event is not raised for a MenuItem that contains submenu items.

Confira também

Aplica-se a