次の方法で共有


Menu.MenuItemCollection.CopyTo メソッド

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

Public Overridable Sub CopyTo( _
   ByVal dest As Array, _   ByVal index As Integer _) Implements ICollection.CopyTo
[C#]
public virtual void CopyTo(Arraydest,intindex);
[C++]
public: virtual void CopyTo(Array* dest,intindex);
[JScript]
public function CopyTo(
   dest : Array,index : int);

パラメータ

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

実装

ICollection.CopyTo

解説

このメソッドを使用すると、複数のコレクションの MenuItem オブジェクトを組み合わせて単一の配列にコピーできます。この機能により、 ContextMenu または MainMenu で使用するために、メニュー項目のグループを簡単に組み合わせることができます。

使用例

[Visual Basic, C#, C++] 配列を作成し、その配列に 2 つの MenuItem オブジェクトから Menu.MenuItemCollection オブジェクトをコピーする例を次に示します。次に、 MenuItem オブジェクトの配列が、 contextMenu1 という名前の ContextMenu コントロール コレクションにコピーされます。この例は、2 つのサブメニュー項目 menuItem1menuItem2 を含む 2 つの MenuItem オブジェクトがあることを前提にしています。

 
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


[C#] 
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);
}


[C++] 
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);
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

参照

Menu.MenuItemCollection クラス | Menu.MenuItemCollection メンバ | System.Windows.Forms 名前空間