語言

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 。

範例

以下程式碼範例建立一個陣列,並將兩個Menu.MenuItemCollection物件的物件複製MenuItem到該陣列中。 接著,範例將物件陣列MenuItem複製到一個命名ContextMenu的控制集合contextMenu1中。 此範例要求有兩個 MenuItem 物件包含名為 menuItem1 和 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 加入集合的末尾。

適用於

另請參閱