Partilhar via


Menu.MenuItemCollection.Clear Método

Definição

Remove todos os objetos MenuItem da coleção de itens de menu.

public:
 virtual void Clear();
public virtual void Clear ();
abstract member Clear : unit -> unit
override this.Clear : unit -> unit
Public Overridable Sub Clear ()

Implementações

Exemplos

Neste exemplo, você cria um menu principal, myMainMenucom dois MenuItem objetos File e Edit. O File menu tem três itens de submenu, New``Opene Exit. Usando o Clear método, você remove todos os MenuItem objetos na coleção de File menus. Este programa exige que você já tenha criado um Form nome 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.
   this->Menu = myMainMenu;
   
   // Clear the File menu items. 
   fileMenu->MenuItems->Clear();
}
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.
    this.Menu = myMainMenu;
    
    // Clear the File menu items. 
    fileMenu.MenuItems.Clear(); 
}
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.
    Me.Menu = myMainMenu

    ' Clear the File menu items. 
    fileMenu.MenuItems.Clear()
End Sub

Comentários

Você pode usar esse método para limpar toda a coleção de itens de menu de um menu. Para remover um item de menu individual da coleção, use o Remove método. Para adicionar novos MenuItem objetos à coleção, use o Add método.

Aplica-se a

Confira também