Menu.MenuItemCollection.CopyTo(Array, Int32) Methode

Definition

Kopiert die gesamte Auflistung an eine angegebene Position in einem vorhandenen Array.

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)

Parameter

dest
Array

Das Zielarray.

index
Int32

Der Index im Zielarray, an dem mit dem Speichern begonnen wird.

Implementiert

Beispiele

Im folgenden Codebeispiel wird ein Array erstellt und die Menu.MenuItemCollection Objekte aus zwei MenuItem Objekten in das Array kopiert. Im Beispiel wird dann das Array von MenuItem -Objekten in die Steuerelementauflistung für einen ContextMenu namens contextMenu1kopiert. In diesem Beispiel müssen zwei MenuItem Objekte vorhanden sein, die Untermenüelemente mit dem Namen menuItem1 und menuItem2enthalten.

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

Hinweise

Sie können diese Methode verwenden, um Objekte aus mehreren Sammlungen in einem einzelnen Array zu kombinieren MenuItem . Mit diesem Feature können Sie problemlos zwei oder mehr Sätze von Menüelementen für die Verwendung in einem ContextMenu oder MainMenukombinieren.

Gilt für: