Menu.MenuItemCollection.IndexOf(MenuItem) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
检索集合中特定项的索引。
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
参数
返回
集合中项的从零开始的索引(如果找到);否则为 -1。
示例
下面的代码示例演示如何创建包含两MenuItem个对象的主菜单myMainMenu
和 File
Edit
两个对象。 菜单File
有三个子菜单:New
和Open``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