Språk

Menu.MenuItemCollection.CopyTo(Array, Int32) Metod

Definition

Kopierar hela samlingen till en befintlig matris på en angiven plats i matrisen.

public:
 virtual void CopyTo(Array ^ dest, int index);
public void CopyTo(Array dest, int index);
abstract member CopyTo : Array * int -> unit
override this.CopyTo : Array * int -> unit
Public Sub CopyTo (dest As Array, index As Integer)

Parametrar

dest
Array

Målmatrisen.

index
Int32

Indexet i målmatrisen där lagringen börjar.

Implementeringar

Exempel

I följande kodexempel skapas en matris och objekt kopieras Menu.MenuItemCollection från två MenuItem objekt till matrisen. Exemplet kopierar sedan matrisen med MenuItem objekt till kontrollsamlingen för en ContextMenu namngiven contextMenu1. Det här exemplet kräver att det finns två MenuItem objekt som innehåller undermenyobjekt med namnet menuItem1 och 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

Kommentarer

Du kan använda den här metoden för att kombinera MenuItem objekt från flera samlingar till en enda matris. Med den här funktionen kan du enkelt kombinera två eller flera uppsättningar menyalternativ för användning i en ContextMenu eller MainMenu.

Gäller för