Menu.MenuItemCollection.AddRange(MenuItem[]) メソッド

定義

以前作成した MenuItem オブジェクトの配列をコレクションに追加します。

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())

パラメーター

items
MenuItem[]

コレクションに追加するメニュー項目を表す MenuItem オブジェクトの配列。

次のコード例では、配列を作成し、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

注釈

メソッドを使用すると、メソッドを使用してコレクションにそれぞれMenuItemを手動で追加する代わりに、以前に作成したMenuItemオブジェクトのグループをすばやくコレクションAddに追加できます。 コレクションに既にオブジェクトが含まれている MenuItem 場合、このメソッドを呼び出すと、新しい MenuItem オブジェクトがコレクションの末尾に追加されます。

適用対象

こちらもご覧ください