Export (0) Print
Expand All

MenuItem.OnClick Method

Raises the Click event.

[Visual Basic]
Protected Overridable Sub OnClick( _
   ByVal e As EventArgs _
)
[C#]
protected virtual void OnClick(
 EventArgs e
);
[C++]
protected: virtual void OnClick(
 EventArgs* e
);
[JScript]
protected function OnClick(
   e : EventArgs
);

Parameters

e
An EventArgs that contains the event data.

Remarks

Raising an event invokes the event handler through a delegate. For more information, see Raising an Event.

Notes to Inheritors:  When overriding OnClick in a derived class, be sure to call the base class's OnClick method.

Example

[Visual Basic, C#, C++] The following example demonstrates how to use the Click event to perform tasks when a MenuItem is clicked. The example creates a MainMenu called mainMenu1 and adds two MenuItem objects, topMenuItem (File) and menuItem1 (Open). It then connects the Click event to the menuItem1_Click event handler. When the user clicks the Open menu item, an OpenFileDialog is initialized and displayed. The example assumes that you have created a Form named Form1.

[Visual Basic] 
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

[C#] 
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();
}

[C++] 
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 = S"&File";
        menuItem1->Text = S"&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, &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 = new OpenFileDialog();
        fd->DefaultExt = S"*.";
        fd->ShowDialog();
    }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

MenuItem Class | MenuItem Members | System.Windows.Forms Namespace

Show:
© 2014 Microsoft