MenuItem.Click 事件

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

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Event Click As EventHandler
用法
Dim instance As MenuItem
Dim handler As EventHandler

AddHandler instance.Click, handler
public event EventHandler Click
public:
event EventHandler^ Click {
    void add (EventHandler^ value);
    void remove (EventHandler^ value);
}
/** @event */
public void add_Click (EventHandler value)

/** @event */
public void remove_Click (EventHandler value)
JScript 支持使用事件,但不支持进行新的声明。

备注

Click 事件在用户单击此 MenuItem 时发生。如果用户使用键盘并按 Enter 键选择菜单项,此事件也会发生。如果按了与 MenuItem 关联的访问键或快捷键,此事件也会发生。有关处理事件的更多信息,请参见 使用事件

提示

如果 MenuItemMenuItems 属性包含任何项,则不会引发该事件。对于父菜单项不会引发该事件。

示例

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

 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
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:
   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.set_Text("&File");
    menuItem1.set_Text("&Open");

    // Add the menu items to the main menu.
    topMenuItem.get_MenuItems().Add(menuItem1);
    mainMenu1.get_MenuItems().Add(topMenuItem);

    // Add functionality to the menu items using the Click event. 
    menuItem1.add_Click(new System.EventHandler(this.menuItem1_Click));

    // Assign mainMenu1 to the form.
    this.set_Menu(mainMenu1);
} //CreateMyMenu

private void menuItem1_Click(Object sender, System.EventArgs e)
{
    // Create a new OpenFileDialog and display it.
    OpenFileDialog fd = new OpenFileDialog();
    fd.set_DefaultExt("*.*");
    fd.ShowDialog();
} //menuItem1_Click

平台

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

请参见

参考

MenuItem 类
MenuItem 成员
System.Windows.Forms 命名空间