Freigeben über


Menu.MenuItemCollection.IndexOf(MenuItem) Methode

Definition

Ruft den Index eines bestimmten Elements in der Auflistung ab.

public:
 int IndexOf(System::Windows::Forms::MenuItem ^ value);
public int IndexOf (System.Windows.Forms.MenuItem value);
member this.IndexOf : System.Windows.Forms.MenuItem -> int
Public Function IndexOf (value As MenuItem) As Integer

Parameter

value
MenuItem

Die MenuItem, die in der Auflistung gesucht werden soll.

Gibt zurück

Int32

Der nullbasierte Index des in der Auflistung gefundenen Elements, andernfalls -1.

Beispiele

Im folgenden Codebeispiel wird gezeigt, wie Sie ein Hauptmenü erstellen, myMainMenumit zwei MenuItem Objekten File und Edit. Das File Menü verfügt über drei Untermenüelemente: New, Openund Exit. Mithilfe der IndexOf Methode rufen Sie den Index des Exit Elements in der File Menüsammlung ab und zeigen dann den Wert in einem Nachrichtenfeld an. In diesem Beispiel ist erforderlich, dass Sie bereits einen Form benannten Namen Form1erstellt haben.

public:
   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;
      
      // Retrieve the index of the Exit menu item.
      String^ indexValue = fileMenu->MenuItems->IndexOf( exitProgram ).ToString();
      
      // Display the result in a message box.
      MessageBox::Show( "The index of the Exit menu item = "
         + indexValue, "MenuItem Information" );
   }
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;
    
    // Retrieve the index of the Exit menu item.
     string indexValue = 
    fileMenu.MenuItems.IndexOf(exitProgram).ToString();
    // Display the result in a message box.
    MessageBox.Show("The index of the Exit menu item = "
            + indexValue, "MenuItem Information");
}
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
   
   ' Retrieve the index of the Exit menu item.
   Dim indexValue As String = fileMenu.MenuItems.IndexOf(exitProgram).ToString()
   ' Display the result in a message box.
   MessageBox.Show("The index of the Exit menu item = " + indexValue, "MenuItem Information")
End Sub

Gilt für