Menu.MenuItemCollection.CopyTo(Array, Int32) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将整个集合复制到数组内指定位置的现有数组中。
public:
virtual void CopyTo(Array ^ dest, int index);
public void CopyTo(Array dest, int index);
abstract member CopyTo : Array * int -> unit
override this.CopyTo : Array * int -> unit
Public Sub CopyTo (dest As Array, index As Integer)
参数
- dest
- Array
目标数组。
- index
- Int32
存储开始的目标数组中的索引。
实现
示例
下面的代码示例创建一个数组,并将 Menu.MenuItemCollection 对象从两 MenuItem 个对象复制到数组中。 然后,该示例将对象的数组复制到命名MenuItem的ContextMenucontextMenu1控件集合中。 此示例要求有两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 到单个数组中。 此功能使你能够轻松组合两组或更多菜单项以供在 ContextMenu 或 MainMenu中使用。