MenuItem.Click 事件

定義

發生於按一下功能表項目,或使用為功能表項目定義的快速鍵或便捷鍵選取時。Occurs when the menu item is clicked or selected using a shortcut key or access key defined for the menu item.

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

事件類型

EventHandler

範例

下列程式碼範例示範如何在 Click 按一下時,使用事件來執行工作 MenuItemThe following code example demonstrates how to use the Click event to perform tasks when a MenuItem is clicked. 此範例會建立 MainMenu 呼叫的 mainMenu1 ,並將兩個 MenuItem 物件 topMenuItem (File) 和 menuItem1 (Open) 。The example creates a MainMenu called mainMenu1 and adds two MenuItem objects, topMenuItem (File) and menuItem1 (Open). 然後,它會將 Click 事件連接至 menuItem1_Click 事件處理常式。It then connects the Click event to the menuItem1_Click event handler. 當使用者按一下 Open 功能表項目時, OpenFileDialog 會初始化並顯示。When the user clicks the Open menu item, an OpenFileDialog is initialized and displayed. 此範例會要求您已建立 Form 名為的 Form1The example requires that you have created a Form named Form1.

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當使用者按一下此專案時,就會發生此事件 MenuItemThe Click event occurs when this MenuItem is clicked by the user. 如果使用者使用鍵盤選取功能表項目,然後按下 Enter 鍵,也會發生此事件。This event also occurs if the user selects the menu item using the keyboard and presses the Enter key. 如果已按下與相關聯的存取金鑰或快速鍵,也可能會發生此情況 MenuItemIt can also occur if an access key or shortcut key is pressed that is associated with the MenuItem. 如需處理事件的詳細資訊,請參閱 處理和引發事件For more information about handling events, see Handling and Raising Events.

注意

如果 MenuItems 的屬性 MenuItem 包含任何專案,則不會引發此事件。If the MenuItems property for the MenuItem contains any items, this event is not raised. 父功能表項目不會引發此事件。This event is not raised for parent menu items.

適用於