Menu.MenuItemCollection.CopyTo(Array, Int32) メソッド

定義

コレクション全体を既存の配列内の指定した位置にコピーします。

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)

パラメーター

dest
Array

コピー先の配列。

index
Int32

コピー先の配列のコピー開始位置を示すインデックス。

実装

次のコード例では、配列を作成し、2 つのMenuItemオブジェクトから配列にオブジェクトをコピーMenu.MenuItemCollectionします。 次に、名前付きcontextMenu1オブジェクトのコントロール コレクションにオブジェクトのMenuItem配列をContextMenuコピーします。 この例では、名前が付いたmenuItem1サブメニュー項目を含む 2 つのMenuItemオブジェクトが必要です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

注釈

このメソッドを使用して、複数のコレクションのオブジェクトを 1 つの配列に結合 MenuItem できます。 この機能を使用すると、2 つ以上のメニュー項目を簡単に組み合わせて、1 つまたは複数 ContextMenu のメニュー項目で使用できます MainMenu

適用対象