共用方式為


MenuItem.OnClick(EventArgs) 方法

定義

引發 Click 事件。

protected:
 virtual void OnClick(EventArgs ^ e);
protected virtual void OnClick (EventArgs e);
abstract member OnClick : EventArgs -> unit
override this.OnClick : EventArgs -> unit
Protected Overridable Sub OnClick (e As EventArgs)

參數

e
EventArgs

包含事件資料的 EventArgs

範例

下列程式碼範例示範如何在按一下 時 MenuItem 使用 Click 事件來執行工作。 此範例會建立名為 mainMenu1 的 ,並新增兩 MenuItemMainMenu 物件, topMenuItem () FilemenuItem1 (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

備註

引發事件會透過委派叫用此事件處理常式。 如需詳細資訊,請參閱 處理和引發事件

給繼承者的注意事項

OnClick(EventArgs) 衍生類別中覆寫時,請務必呼叫基類的 OnClick(EventArgs) 方法。

適用於