แก้ไข

แชร์ผ่าน


MenuItemCollection.CopyTo Method

Definition

Copies the contents of the current MenuItemCollection object.

Overloads

CopyTo(Array, Int32)

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)

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)

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)

Parameters

array
Array

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.

Implements

Exceptions

array is not an array of MenuItem objects.

Remarks

Use 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. With the System.Array, you can then use array syntax to access the items in the MenuItemCollection object.

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.

See also

Applies to

CopyTo(MenuItem[], Int32)

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)

Parameters

array
MenuItem[]

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.

Examples

The 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>

Remarks

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. With the array, you can then use array syntax to access the items in the MenuItemCollection object.

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.

See also

Applies to