Menu.MenuItemCollection.Clear Metodo

Definizione

Rimuove tutti gli oggetti MenuItem dall'insieme delle voci di menu.

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

Implementazioni

Esempio

In questo esempio viene creato un menu principale, , myMainMenucon due MenuItem oggetti File e Edit. Il File menu include tre sottomenu elementi, New, Opene Exit. Usando il Clear metodo, rimuovere tutti gli MenuItem oggetti nella File raccolta di menu. Questo programma richiede che sia già stato creato un Form oggetto denominato 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

Commenti

È possibile usare questo metodo per cancellare l'intera raccolta di voci di menu da un menu. Per rimuovere una singola voce di menu dalla raccolta, usare il Remove metodo . Per aggiungere nuovi MenuItem oggetti alla raccolta, usare il Add metodo .

Si applica a

Vedi anche