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 개체의 배열입니다.

예제

다음 코드 예제에서는 배열을 만들고 두 MenuItem 개체의 Menu.MenuItemCollection 개체를 배열에 복사합니다. 그런 다음, 개체 배열 MenuItem 을 명명contextMenu1된 컨트롤 컬렉션에 복사합니다ContextMenu. 이 예제에서는 명명 menuItem1 된 하위 메뉴 항목과 menuItem2하위 메뉴 항목을 포함하는 두 개의 MenuItem 개체가 있어야 합니다.

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 개체가 추가됩니다.

적용 대상

추가 정보