Menu.MenuItemCollection.RemoveAt(Int32) 方法

定义

从菜单项集合的指定索引位置移除 MenuItem

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

参数

index
Int32

要移除的 MenuItem 的索引。

实现

示例

下面的代码示例演示如何创建包含两MenuItem个对象的主菜单myMainMenuFile Edit两个对象。 菜单File有三个子菜单:NewOpen``Exit。 通过使用RemoveAt该方法,可以使用其索引号从File菜单集合中删除Exit该项。 此示例要求你已创建一个 Form 命名 Form1的 。

void InitializeMyMenu()
{
   // Create the MainMenu object.
   MainMenu^ myMainMenu = gcnew MainMenu;

   // Create the MenuItem objects.
   MenuItem^ fileMenu = gcnew MenuItem( "&File" );
   MenuItem^ editMenu = gcnew MenuItem( "&Edit" );
   MenuItem^ newFile = gcnew MenuItem( "&New" );
   MenuItem^ openFile = gcnew MenuItem( "&Open" );
   MenuItem^ exitProgram = gcnew MenuItem( "E&xit" );

   // Add the MenuItem objects to myMainMenu.
   myMainMenu->MenuItems->Add( fileMenu );
   myMainMenu->MenuItems->Add( editMenu );

   // Add three submenus to the File menu.
   fileMenu->MenuItems->Add( newFile );
   fileMenu->MenuItems->Add( openFile );
   fileMenu->MenuItems->Add( exitProgram );

   // Assign myMainMenu to the form.
   Menu = myMainMenu;

   // Remove the item S"Exit" from the File menu.
   fileMenu->MenuItems->RemoveAt( 2 );
}
public void InitializeMyMenu()
{
    // Create the MainMenu object.
    MainMenu myMainMenu = new MainMenu();
    
    // Create the MenuItem objects.
    MenuItem fileMenu = new MenuItem("&File");
    MenuItem editMenu = new MenuItem("&Edit");
    MenuItem newFile = new MenuItem("&New");
    MenuItem openFile = new MenuItem("&Open");
    MenuItem exitProgram = new MenuItem("E&xit");
    
    // Add the MenuItem objects to myMainMenu.
    myMainMenu.MenuItems.Add(fileMenu);
    myMainMenu.MenuItems.Add(editMenu);
    
    // Add three submenus to the File menu.
    fileMenu.MenuItems.Add(newFile);
    fileMenu.MenuItems.Add(openFile);
    fileMenu.MenuItems.Add(exitProgram);
    
    // Assign myMainMenu to the form.
    Menu = myMainMenu;
    
    // Remove the item "Exit" from the File menu. 
    fileMenu.MenuItems.RemoveAt(2);
}
Public Sub InitializeMyMenu()
   ' Create the MainMenu object.
   Dim myMainMenu As New MainMenu()
   
   ' Create the MenuItem objects.
   Dim fileMenu As New MenuItem("&File")
   Dim editMenu As New MenuItem("&Edit")
   Dim newFile As New MenuItem("&New")
   Dim openFile As New MenuItem("&Open")
   Dim exitProgram As New MenuItem("E&xit")
   
   ' Add the MenuItem objects to myMainMenu.
   myMainMenu.MenuItems.Add(fileMenu)
   myMainMenu.MenuItems.Add(editMenu)
   
   ' Add three submenus to the File menu.
   fileMenu.MenuItems.Add(newFile)
   fileMenu.MenuItems.Add(openFile)
   fileMenu.MenuItems.Add(exitProgram)
   
   ' Assign myMainMenu to the form.
   Menu = myMainMenu
   
   ' Remove the item "Exit" from the File menu. 
   fileMenu.MenuItems.RemoveAt(2)
End Sub

注解

从菜单项集合中删除 a MenuItem 后,所有后续菜单项都将在集合中向上移动一个位置。

适用于