共用方式為


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。

範例

下列程式碼範例示範如何建立主功能表 , myMainMenu 其中包含兩 MenuItem 個 物件和 File Edit 。 功能表 File 有三個子功能表項目目: NewOpenExit 。 藉由使用 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

適用於