Menu.MenuItemCollection.Contains(MenuItem) 方法

定义

确定指定的 MenuItem 是否是集合的成员。

public:
 bool Contains(System::Windows::Forms::MenuItem ^ value);
public bool Contains (System.Windows.Forms.MenuItem value);
member this.Contains : System.Windows.Forms.MenuItem -> bool
Public Function Contains (value As MenuItem) As Boolean

参数

value
MenuItem

要在集合中查找的 MenuItem

返回

Boolean

如果 MenuItem 是集合的成员,则为 true;否则为 false

示例

在此示例中,你将创建一个主菜单, myMainMenu其中包含两 MenuItem 个对象和 File Edit两个对象。 菜单File有三个子菜单项,New``Open以及Exit。 通过使用该方法 Contains ,检查菜单集合是否 File 包含该项 Open。 如果项存在,将在文本框中显示结果。 此程序要求你已创建一个 Form 命名 Form1的 。

public:
   void InitializeMenu()
   {
      // 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;
      
      // Check that the File menu contains the Open menu item.
      if ( fileMenu->MenuItems->Contains( openFile ) )
      {
         MessageBox::Show( "The File menu contains 'Open' " + fileMenu->Text );
      }
   }
public void InitializeMenu()
{
    // 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;
    
    // Check that the File menu contains the Open menu item.
    if (fileMenu.MenuItems.Contains(openFile))
    {
        MessageBox.Show("The File menu contains 'Open' ", fileMenu.Text);
    }
}
Public Sub InitializeMenu()
   ' 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
   
   ' Check that the File menu contains the Open menu item.
   If fileMenu.MenuItems.Contains(openFile) Then
      MessageBox.Show("The File menu contains 'Open' ", fileMenu.Text)
   End If
End Sub 
'InitializeMenu

注解

此方法使你能够在尝试对MenuItem集合执行操作之前确定是否MenuItem是集合的成员。 可以使用此方法确认 MenuItem 已添加到集合中还是仍然是集合的成员。

适用于