Menu.MenuItemCollection.IndexOf(MenuItem) 方法

定义

检索集合中特定项的索引。

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

参数

value
MenuItem

要在集合中查找的 MenuItem

返回

Int32

集合中项的从零开始的索引(如果找到);否则为 -1。

示例

下面的代码示例演示如何创建包含两MenuItem个对象的主菜单myMainMenuFile Edit两个对象。 菜单File有三个子菜单:NewOpen``Exit。 通过使用IndexOf该方法,检索菜单集合中File项的Exit索引,然后在消息框中显示其值。 此示例要求你已创建一个 Form 命名 Form1的 。

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

适用于