MenuItemStyleCollection.RemoveAt(Int32) 方法

定義

從集合中移除指定索引位置的 MenuItemStyle 物件。

public:
 void RemoveAt(int index);
public void RemoveAt (int index);
member this.RemoveAt : int -> unit
Public Sub RemoveAt (index As Integer)

參數

index
Int32

要移除的 MenuItemStyle 之以零起始的索引位置。

範例

下列程式碼範例示範如何使用 MenuItemStyleCollection 類別,根據功能表項目的層級,在控制項中 Menu 指定功能表項目的樣式設定。 在此範例中 LevelMenuItemStyles ,會以宣告方式建立 屬性,並移除一個物件,並將另一個 MenuItemStyle 物件新增至 MenuItemStyleCollection 物件。


<%@ 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)
  {

    if (!IsPostBack)
    {

      // Use the Add and RemoveAt methods to programmatically 
      // remove the third level menu item style and replace 
      // it with a new style, in this case replacing the green background
        // and yellow text with the blue background and white text. 

        MenuItemStyle newStyle = new MenuItemStyle();
        newStyle.BackColor = System.Drawing.Color.Blue;
        newStyle.ForeColor = System.Drawing.Color.White;

        // Remove the last of the three menu item styles. Note that
        // since the collection has a zero-based index, the third
        // entry has an index value of 2.
        MainMenuID.LevelMenuItemStyles.RemoveAt(2);
        MainMenuID.LevelMenuItemStyles.Add(newStyle);

    }

  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>MenuItemStyleCollection Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>MenuItemStyleCollection Example</h3>
         <!--Add MenuItemStyle objects to the MenuItemStyleCollection -->
         <!--using LevelMenuItemStyles.  -->
         <!--Note that each menu item style represents a level in the menu -->

      <asp:Menu id="MainMenuID"
       Font-Names= "Arial"
        ForeColor="Blue"
        runat="server">
         
         <LevelMenuItemStyles>
         <asp:MenuItemStyle BackColor="Azure" 
             Font-Italic="true"
             Font-Names="Arial"
             ForeColor="Black" />
         
           <asp:MenuItemStyle BackColor="Black" 
             Font-Italic="false"
             Font-Names="Arial"
             ForeColor="White" />
             
         <asp:MenuItemStyle BackColor="Green" 
             Font-Italic="true"
             Font-Names="Arial"
             ForeColor="Yellow" />
       
         </LevelMenuItemStyles>

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

    </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) 
    
    If Not IsPostBack Then
        
        ' Use the Add and RemoveAt methods to programmatically 
        ' remove the third level menu item style and replace 
        ' it with a new style, in this case replacing the green background
        ' and yellow text with the blue background and white text. 
        Dim newStyle As New MenuItemStyle()
        newStyle.BackColor = System.Drawing.Color.Blue
        newStyle.ForeColor = System.Drawing.Color.White
        
        ' Remove the last of the three menu item styles. Note that
        ' since the collection has a zero-based index, the third
        ' entry has an index value of 2.
        MainMenuID.LevelMenuItemStyles.RemoveAt(2)
        MainMenuID.LevelMenuItemStyles.Add(newStyle)
    End If
 
End Sub 'Page_Load
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>MenuItemStyleCollection Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>MenuItemStyleCollection Example</h3>
         <!--Add MenuItemStyle objects to the MenuItemStyleCollection -->
         <!--using LevelMenuItemStyles.   -->
         <!--Note that each menu item style represents a level in the menu -->

      <asp:Menu id="MainMenuID"
       Font-Names= "Arial"
        ForeColor="Blue"
        runat="server">
         
         <LevelMenuItemStyles>
         <asp:MenuItemStyle BackColor="Azure" 
             Font-Italic="true"
             Font-Names="Arial"
             ForeColor="Black" />
         
           <asp:MenuItemStyle BackColor="Black" 
             Font-Italic="false"
             Font-Names="Arial"
             ForeColor="White" />
             
         <asp:MenuItemStyle BackColor="Green" 
             Font-Italic="true"
             Font-Names="Arial"
             ForeColor="Yellow" />
       
         </LevelMenuItemStyles>

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

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

備註

RemoveAt使用 方法,從集合中移除 MenuItemStyle 位於指定之以零起始的索引位置的物件。 接著,該節點之後的所有專案都會向上移動,以填滿空的位置。 移動專案的索引也會更新。

或者,您可以使用 方法移除特定 MenuItemStyle 物件 Remove

適用於

另請參閱