Freigeben über


Menu.MenuItemCollection.AddRange-Methode

Fügt der Auflistung ein Array von zuvor erstellten MenuItem-Objekten hinzu.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

Syntax

'Declaration
Public Overridable Sub AddRange ( _
    items As MenuItem() _
)
'Usage
Dim instance As MenuItemCollection
Dim items As MenuItem()

instance.AddRange(items)
public virtual void AddRange (
    MenuItem[] items
)
public:
virtual void AddRange (
    array<MenuItem^>^ items
)
public void AddRange (
    MenuItem[] items
)
public function AddRange (
    items : MenuItem[]
)

Parameter

  • items
    Ein Array von MenuItem-Objekten, die die der Auflistung hinzuzufügenden Menüelemente darstellen.

Hinweise

Mit dieser Methode können Sie der Auflistung eine Gruppe zuvor erstellter MenuItem-Objekte schnell hinzufügen, statt mit der Add-Methode jedes MenuItem manuell hinzuzufügen. Wenn die Auflistung bereits MenuItem-Objekte enthält, werden durch Aufruf dieser Methode neue MenuItem-Objekte am Ende der Auflistung hinzugefügt.

Beispiel

Im folgenden Codebeispiel wird ein Array erstellt, und die Menu.MenuItemCollection-Objekte aus zwei MenuItem-Objekten werden in das Array kopiert. Im Beispiel wird das Array von MenuItem-Objekten in die Steuerelementauflistung für ein ContextMenucontextMenu1 kopiert. Im Beispiel müssen zwei MenuItem-Objekte vorhanden sein, die die Untermenüelemente menuItem1 und menuItem2 enthalten.

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
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:
   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 );
   }

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

Siehe auch

Referenz

Menu.MenuItemCollection-Klasse
Menu.MenuItemCollection-Member
System.Windows.Forms-Namespace
Add
CopyTo