MenuItemCollection.CopyTo 方法

定义

复制当前 MenuItemCollection 对象的内容。Copies the contents of the current MenuItemCollection object.

重载

CopyTo(Array, Int32)

从目标数组的指定索引开始,将 MenuItemCollection 对象中的所有项复制到兼容的一维 Array 中。Copies all the items from the MenuItemCollection object to a compatible one-dimensional Array, starting at the specified index in the target array.

CopyTo(MenuItem[], Int32)

从目标数组的指定索引开始,将 MenuItemCollection 对象中的所有项复制到 MenuItem 对象的一维兼容数组。Copies all the items from the MenuItemCollection object to a compatible one-dimensional array of MenuItem objects, starting at the specified index in the target array.

CopyTo(Array, Int32)

从目标数组的指定索引开始,将 MenuItemCollection 对象中的所有项复制到兼容的一维 Array 中。Copies all the items from the MenuItemCollection object to a compatible one-dimensional Array, starting at the specified index in the target array.

public:
 virtual void CopyTo(Array ^ array, int index);
public void CopyTo (Array array, int index);
abstract member CopyTo : Array * int -> unit
override this.CopyTo : Array * int -> unit
Public Sub CopyTo (array As Array, index As Integer)

参数

array
Array

从零开始的 Array,它接收从当前 MenuItemCollection 复制的项。A zero-based Array that receives the copied items from the current MenuItemCollection.

index
Int32

目标数组中开始接收复制内容的位置。The position in the target array at which to start receiving the copied content.

实现

例外

array 不是 MenuItem 对象的数组。array is not an array of MenuItem objects.

注解

使用 CopyTo 方法将当前对象的内容复制 MenuItemCollection 到指定的从零开始的 System.ArrayUse the CopyTo method to copy the contents of the current MenuItemCollection object into the specified zero-based System.Array. 从目标数组的指定索引处开始复制项。Items are copied starting at the specified index of the target array. 利用 System.Array ,你可以使用数组语法来访问对象中的项 MenuItemCollectionWith the System.Array, you can then use array syntax to access the items in the MenuItemCollection object.

作为替代方法,还可以使用 GetEnumerator 方法来创建可用于访问集合中的项的枚举器。As an alternative, you can also use the GetEnumerator method to create an enumerator that can be used to access the items in the collection.

另请参阅

适用于

CopyTo(MenuItem[], Int32)

从目标数组的指定索引开始,将 MenuItemCollection 对象中的所有项复制到 MenuItem 对象的一维兼容数组。Copies all the items from the MenuItemCollection object to a compatible one-dimensional array of MenuItem objects, starting at the specified index in the target array.

public:
 void CopyTo(cli::array <System::Web::UI::WebControls::MenuItem ^> ^ array, int index);
public void CopyTo (System.Web.UI.WebControls.MenuItem[] array, int index);
member this.CopyTo : System.Web.UI.WebControls.MenuItem[] * int -> unit
Public Sub CopyTo (array As MenuItem(), index As Integer)

参数

array
MenuItem[]

MenuItem 对象的从零开始的数组,它接收从当前 MenuItemCollection 复制的项。A zero-based array of MenuItem objects that receives the copied items from the current MenuItemCollection.

index
Int32

目标数组中开始接收复制内容的位置。The position in the target array at which to start receiving the copied content.

示例

下面的代码示例演示如何使用方法将 CopyTo 对象中的项复制 MenuItemCollection 到对象的数组中 MenuItemThe following code example demonstrates how to use the CopyTo method to copy the items in a MenuItemCollection object to an array of MenuItem objects.


<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    
  void Page_Load(Object sender, EventArgs e)
  {

    // Display the submenu items of the Music
    // menu item. 

    // Retrieve the Music menu item.
    MenuItem musicMenuItem = NavigationMenu.FindItem(@"Home");

    // Declare an array of MenuItem objects.
         MenuItem[] musicItemArray = new MenuItem[musicMenuItem.ChildItems.Count];

    // Use the CopyTo method to copy the submenu items 
    // of the Music menu item into the array.
    musicMenuItem.ChildItems.CopyTo(musicItemArray, 0);
    
    // Display the menu items.
    Message.Text = "The submenu items of the Home menu item are: <br/><br/>";

    foreach (MenuItem item in musicItemArray)
    {

      Message.Text += item.Text + "<br />";

    }

  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>MenuItemCollection CopyTo Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>MenuItemCollection CopyTo Example</h3>
    
      <asp:menu id="NavigationMenu"
        orientation="Vertical"
        target="_blank" 
        runat="server">
        
        <items>
          <asp:menuitem text="Home"
            tooltip="Home">
            <asp:menuitem text="Music"
              tooltip="Music">
              <asp:menuitem text="Classical"
                tooltip="Classical"/>
              <asp:menuitem text="Rock"
                tooltip="Rock"/>
              <asp:menuitem text="Jazz"
                tooltip="Jazz"/>
            </asp:menuitem>
            <asp:menuitem text="Movies"
              tooltip="Movies">
              <asp:menuitem text="Action"
                tooltip="Action"/>
              <asp:menuitem text="Drama"
                tooltip="Drama"/>
              <asp:menuitem text="Musical"
                tooltip="Musical"/>
            </asp:menuitem>
          </asp:menuitem>
        </items>

      </asp:menu>
      
      <hr/>

      <asp:label id="Message" 
        runat="server"/>

    </form>
  </body>
</html>


<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    
  Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    ' Display the submenu items of the Music
    ' menu item. 

    ' Retrieve the Music menu item.
    Dim musicMenuItem As MenuItem = NavigationMenu.FindItem("Home")

    ' Declare an array of MenuItem objects.
    Dim musicItemArray(musicMenuItem.ChildItems.Count - 1) As MenuItem
    
    ' Use the CopyTo method to copy the submenu items 
    ' of the Music menu item into the array.
    musicMenuItem.ChildItems.CopyTo(musicItemArray, 0)
    
    ' Display the menu items.
        Message.Text = "The submenu items of the Home menu item are: <br/><br/>"
    
    Dim item As MenuItem
    For Each item In musicItemArray
    
      Message.Text &= item.Text & "<br />"

    Next

  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>MenuItemCollection CopyTo Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>MenuItemCollection CopyTo Example</h3>
    
      <asp:menu id="NavigationMenu"
        orientation="Vertical"
        target="_blank" 
        runat="server">
        
        <items>
          <asp:menuitem text="Home"
            tooltip="Home">
            <asp:menuitem text="Music"
              tooltip="Music">
              <asp:menuitem text="Classical"
                tooltip="Classical"/>
              <asp:menuitem text="Rock"
                tooltip="Rock"/>
              <asp:menuitem text="Jazz"
                tooltip="Jazz"/>
            </asp:menuitem>
            <asp:menuitem text="Movies"
              tooltip="Movies">
              <asp:menuitem text="Action"
                tooltip="Action"/>
              <asp:menuitem text="Drama"
                tooltip="Drama"/>
              <asp:menuitem text="Musical"
                tooltip="Musical"/>
            </asp:menuitem>
          </asp:menuitem>
        </items>

      </asp:menu>
      
      <hr/>

      <asp:label id="Message" 
        runat="server"/>

    </form>
  </body>
</html>

注解

使用 CopyTo 方法将当前对象的内容复制 MenuItemCollection 到指定的从零开始的数组中。Use the CopyTo method to copy the contents of the current MenuItemCollection object into the specified zero-based array. 从目标数组的指定索引处开始复制项。Items are copied starting at the specified index of the target array. 使用数组时,可以使用数组语法来访问对象中的项 MenuItemCollectionWith the array, you can then use array syntax to access the items in the MenuItemCollection object.

作为替代方法,还可以使用 GetEnumerator 方法来创建可用于访问集合中的项的枚举器。As an alternative, you can also use the GetEnumerator method to create an enumerator that can be used to access the items in the collection.

另请参阅

适用于