Export (0) Print
Expand All

MenuItem.OnClick Method

Raises the Click event.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

protected virtual void OnClick (
	EventArgs e
)
protected void OnClick (
	EventArgs e
)
protected function OnClick (
	e : EventArgs
)

Parameters

e

An EventArgs that contains the event data.

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.

The following code 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 requires that you have created a Form named Form1.

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 = 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

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft