Menu.MenuItemCollection.AddRange(MenuItem[]) Metoda

Definicja

Dodaje tablicę wcześniej utworzonych MenuItem obiektów do kolekcji.

public:
 virtual void AddRange(cli::array <System::Windows::Forms::MenuItem ^> ^ items);
public virtual void AddRange (System.Windows.Forms.MenuItem[] items);
abstract member AddRange : System.Windows.Forms.MenuItem[] -> unit
override this.AddRange : System.Windows.Forms.MenuItem[] -> unit
Public Overridable Sub AddRange (items As MenuItem())

Parametry

items
MenuItem[]

Tablica MenuItem obiektów reprezentujących elementy menu do dodania do kolekcji.

Przykłady

Poniższy przykład kodu tworzy tablicę i kopiuje Menu.MenuItemCollection obiekty z dwóch MenuItem obiektów do tablicy. W tym przykładzie zostanie skopiowana tablica MenuItem obiektów do kolekcji kontrolek o ContextMenu nazwie contextMenu1. W tym przykładzie wymagane są dwa MenuItem obiekty zawierające elementy podrzędne o nazwie menuItem1 i menuItem2.

private:
   void CopyMyMenus()
   {
      // Create empty array to store MenuItem objects.
      array<MenuItem^>^ myItems = gcnew array<MenuItem^>(
         menuItem1->MenuItems->Count + menuItem2->MenuItems->Count );
      
      // Copy elements of the first MenuItem collection to array.
      menuItem1->MenuItems->CopyTo( myItems, 0 );
      // Copy elements of the second MenuItem collection, after the first set.
      menuItem2->MenuItems->CopyTo( myItems, myItems->Length );
      
      // Add the array to the menu item collection of the ContextMenu.
      contextMenu1->MenuItems->AddRange( myItems );
   }
private void CopyMyMenus()
{
   // Create empty array to store MenuItem objects.
   MenuItem[] myItems = 
      new MenuItem[menuItem1.MenuItems.Count + menuItem2.MenuItems.Count];
   
   // Copy elements of the first MenuItem collection to array.
   menuItem1.MenuItems.CopyTo(myItems, 0);
   // Copy elements of the second MenuItem collection, after the first set.
   menuItem2.MenuItems.CopyTo(myItems, myItems.Length);

   // Add the array to the menu item collection of the ContextMenu.
   contextMenu1.MenuItems.AddRange(myItems);
}
Private Sub CopyMyMenus()
    ' Create empty array to store MenuItem objects.
    Dim myItems(menuItem1.MenuItems.Count + menuItem2.MenuItems.Count) As MenuItem
       
    ' Copy elements of the first MenuItem collection to array.
    menuItem1.MenuItems.CopyTo(myItems, 0)
    ' Copy elements of the second MenuItem collection, after the first set.
    menuItem2.MenuItems.CopyTo(myItems, myItems.Length)
       
    ' Add the array to the menu item collection of the ContextMenu.
    contextMenu1.MenuItems.AddRange(myItems)
End Sub

Uwagi

Za pomocą metody można szybko dodać grupę wcześniej utworzonych MenuItem obiektów do kolekcji zamiast ręcznie dodawać je MenuItem do kolekcji przy użyciu Add metody . Jeśli kolekcja zawiera MenuItem już obiekty, wywołanie tej metody spowoduje dodanie nowych MenuItem obiektów na końcu kolekcji.

Dotyczy

Zobacz też