MenuItem.Click 事件

定义

当单击菜单项或使用为该菜单项定义的快捷键或访问键选择菜单项时发生。

public:
 event EventHandler ^ Click;
public event EventHandler Click;
member this.Click : EventHandler 
Public Custom Event Click As EventHandler 

事件类型

示例

下面的代码示例演示如何在单击 时MenuItem使用 Click 事件执行任务。 该示例创建一个名为 的 ,并添加两MenuItemMainMenumainMenu1 对象, topMenuItem (File) 和 menuItem1 (Open) 。 然后,它将 Click 事件连接到 menuItem1_Click 事件处理程序。 当用户单击菜单项时 OpenOpenFileDialog 将初始化并显示 。 该示例要求已创建名为 FormForm1

public:
   void CreateMyMenu()
   {
      // Create a main menu object.
      MainMenu^ mainMenu1 = gcnew MainMenu;

      // Create empty menu item objects.
      MenuItem^ topMenuItem = gcnew MenuItem;
      MenuItem^ menuItem1 = gcnew MenuItem;

      // Set the caption of the menu items.
      topMenuItem->Text = "&File";
      menuItem1->Text = "&Open";

      // Add the menu items to the main menu.
      topMenuItem->MenuItems->Add( menuItem1 );
      mainMenu1->MenuItems->Add( topMenuItem );

      // Add functionality to the menu items using the Click event. 
      menuItem1->Click += gcnew System::EventHandler( this, &Form1::menuItem1_Click );

      // Assign mainMenu1 to the form.
      this->Menu = mainMenu1;
   }

private:
   void menuItem1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      // Create a new OpenFileDialog and display it.
      OpenFileDialog^ fd = gcnew OpenFileDialog;
      fd->DefaultExt = "*.";
      fd->ShowDialog();
   }
public void CreateMyMenu()
{
    // Create a main menu object.
    MainMenu mainMenu1 = new MainMenu();

    // Create empty menu item objects.
    MenuItem topMenuItem = new MenuItem();
    MenuItem menuItem1 = new MenuItem();
          
    // Set the caption of the menu items.
    topMenuItem.Text = "&File";
    menuItem1.Text = "&Open";

    // Add the menu items to the main menu.
        topMenuItem.MenuItems.Add(menuItem1);
    mainMenu1.MenuItems.Add(topMenuItem);
                
    // Add functionality to the menu items using the Click event. 
    menuItem1.Click += new System.EventHandler(this.menuItem1_Click);

    // Assign mainMenu1 to the form.
    this.Menu=mainMenu1;
}

private void menuItem1_Click(object sender, System.EventArgs e)
{	
           // Create a new OpenFileDialog and display it.
   OpenFileDialog fd = new OpenFileDialog();
       fd.DefaultExt = "*.*";
   fd.ShowDialog();
}
 Public Sub CreateMyMenu()
   ' Create a main menu object.
   Dim mainMenu1 As New MainMenu()

   ' Create empty menu item objects.
   Dim topMenuItem As New MenuItem()
   Dim menuItem1 As New MenuItem()

   ' Set the caption of the menu items.
   topMenuItem.Text = "&File"
   menuItem1.Text = "&Open"

   ' Add the menu items to the main menu.
   topMenuItem.MenuItems.Add(menuItem1)
   mainMenu1.MenuItems.Add(topMenuItem)

   ' Add functionality to the menu items using the Click event. 
   AddHandler menuItem1.Click, AddressOf Me.menuItem1_Click
   ' Assign mainMenu1 to the form.
   Me.Menu = mainMenu1
End Sub


Private Sub menuItem1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
   ' Create a new OpenFileDialog and display it.
   Dim fd As New OpenFileDialog()
   fd.DefaultExt = "*.*"
   fd.ShowDialog()
End Sub

注解

当用户 Click 单击此 MenuItem 事件时发生。 如果用户使用键盘选择菜单项并按 Enter 键,也会发生此事件。 如果按下与 关联的访问键或快捷键,也可能发生此 MenuItem情况。 有关处理事件的详细信息,请参阅 处理和引发事件

注意

MenuItems如果 的 MenuItem 属性包含任何项,则不会引发此事件。 不会为父菜单项引发此事件。

适用于