Edytuj

Udostępnij za pośrednictwem


MenuItemCollection.GetEnumerator Method

Definition

Returns an enumerator that can be used to iterate through the items in the current MenuItemCollection object.

public:
 virtual System::Collections::IEnumerator ^ GetEnumerator();
public System.Collections.IEnumerator GetEnumerator ();
abstract member GetEnumerator : unit -> System.Collections.IEnumerator
override this.GetEnumerator : unit -> System.Collections.IEnumerator
Public Function GetEnumerator () As IEnumerator

Returns

An enumerator that can be used to iterate through the items in the current MenuItemCollection.

Implements

Examples

The following code example demonstrates how to use the GetEnumerator method to create an enumerator that contains the submenu items of the Music menu item in a Menu control.


<%@ 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\Music");

    // Use the GetEnumerator method to create an enumerator 
    // that contains the submenu items of the Music menu item.
    IEnumerator menuItemEnumerator = musicMenuItem.ChildItems.GetEnumerator();

    Message.Text = "The submenu items of the Music menu item are: <br/><br/>";

    // Iterate though the enumerator to display the menu items.
    while (menuItemEnumerator.MoveNext())
    {

      Message.Text += ((MenuItem)(menuItemEnumerator.Current)).Text + "<br />";

    }

  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>MenuItemCollection GetEnumerator Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>MenuItemCollection GetEnumerator 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\Music")

    ' Use the GetEnumerator method to create an enumerator 
    ' that contains the submenu items of the Music menu item.
    Dim menuItemEnumerator As IEnumerator = musicMenuItem.ChildItems.GetEnumerator()

    Message.Text = "The submenu items of the Music menu item are: <br/><br/>"

    ' Iterate though the enumerator to display the menu items.
    While menuItemEnumerator.MoveNext()

      Message.Text &= (CType(menuItemEnumerator.Current, MenuItem)).Text & "<br />"

    End While

  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>MenuItemCollection GetEnumerator Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>MenuItemCollection GetEnumerator 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 GetEnumerator method to create an enumerator that can be easily iterated through to get each item in the current MenuItemCollection object. To get the item currently pointed to in the enumerator, use the Current property. Use the MoveNext method to move to the next item. If you need to move the enumerator back to the beginning of the collection, use the Reset method.

Note

After you create an enumerator or use the Reset method, you must call the MoveNext method. Otherwise, the item represented by the Current property is undefined.

As an alternative, you can also use the CopyTo method to copy the items in the collection to an array. You can then use the array to access the items in the collection.

Applies to

See also