다음을 통해 공유


Menu.MenuItemCollection.IndexOf 메서드

컬렉션에서 특정 항목의 인덱스를 검색합니다.

네임스페이스: System.Windows.Forms
어셈블리: System.Windows.Forms(system.windows.forms.dll)

구문

‘선언
Public Function IndexOf ( _
    value As MenuItem _
) As Integer
‘사용 방법
Dim instance As MenuItemCollection
Dim value As MenuItem
Dim returnValue As Integer

returnValue = instance.IndexOf(value)
public int IndexOf (
    MenuItem value
)
public:
int IndexOf (
    MenuItem^ value
)
public int IndexOf (
    MenuItem value
)
public function IndexOf (
    value : MenuItem
) : int

매개 변수

  • value
    컬렉션에서 찾을 MenuItem입니다.

반환 값

항목이 컬렉션에서 있으면 해당 항목의 0부터 시작하는 인덱스이고, 그렇지 않으면 -1입니다.

예제

다음 코드 예제에서는 FileEdit라는 두 개의 MenuItem 개체를 사용하여 myMainMenu라는 주 메뉴를 만드는 방법을 보여 줍니다. File 메뉴에는 New, OpenExit라는 세 개의 하위 메뉴 항목이 있습니다. IndexOf 메서드를 사용하여 File 메뉴 컬렉션에서 Exit 항목의 인덱스를 검색한 다음 그 값을 메시지 상자에 표시합니다. 이 예제를 실행하려면 Form1이라는 Form을 만든 상태여야 합니다.

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 'InitializeMyMenu
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:
   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" );
   }

플랫폼

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0, 1.0에서 지원

참고 항목

참조

Menu.MenuItemCollection 클래스
Menu.MenuItemCollection 멤버
System.Windows.Forms 네임스페이스