Partilhar via


Menu.MenuItemCollection.RemoveAt(Int32) Método

Definição

Remove um MenuItem da coleção de itens de menu em um índice especificado.

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)

Parâmetros

index
Int32

O índice do MenuItem a ser removido.

Implementações

Exemplos

O exemplo de código a seguir mostra como criar um menu principal, myMainMenu, com dois MenuItem objetos File e Edit. O File menu tem três itens de submenu: New, Opene Exit. Usando o RemoveAt método , você remove o Exit item da coleção de File menus usando seu número de índice. Este exemplo requer que você já tenha criado um Form chamado 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

Comentários

Quando um MenuItem é removido da coleção de itens de menu, todos os itens de menu subsequentes são movidos para cima uma posição na coleção.

Aplica-se a