Export (0) Print
Expand All

MenuItem.PerformClick Method

Generates a Click event for the MenuItem, simulating a click by a user.

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

public void PerformClick ()
public void PerformClick ()
public function PerformClick ()
Not applicable.

You can use this menu to activate a menu item through code without passing any event information. For example, if you want to activate a menu item based on an action that occurs in your application, you can call the PerformClick method for that MenuItem.

In this example you programmatically click a menu item by using the PerformClick method. First, you create a main menu (mainMenu1) and add to it two menu items, menuItem1 (File) and menuItem2 (Edit). You also use the Click event to send data to the event handler when a menu item is clicked. Then you use the PerformClick method to click the File menu item. When you start the application, the File menu item is activated, and a message box that contains the text "The File menu is clicked." appears on the screen. 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 menuItem1 = new MenuItem();
    MenuItem menuItem2 = new MenuItem();

    // Set the caption of the menu items.
    menuItem1.Text = "&File";
    menuItem2.Text = "&Edit";

    // Add the menu items to the main menu.
    mainMenu1.MenuItems.Add(menuItem1);
    mainMenu1.MenuItems.Add(menuItem2);
    
    // Add functionality to the menu items. 
    menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
    menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
    
    // Assign mainMenu1 to the form.
    this.Menu=mainMenu1;

    // Perform a click on the File menu item.
    menuItem1.PerformClick();
}

private void menuItem1_Click(object sender, System.EventArgs e)
{    
    MessageBox.Show("You clicked the File menu.","The Event Information");        
}

private void menuItem2_Click(object sender, System.EventArgs e)
{
    MessageBox.Show("You clicked the Edit menu.","The Event Information");        
}

    // Create a main menu object.
    MainMenu mainMenu1 = new MainMenu();
    // Create empty menu item objects.
    MenuItem menuItem1 = new MenuItem();
    MenuItem menuItem2 = new MenuItem();
    // Set the caption of the menu items.
    menuItem1.set_Text("&File");
    menuItem2.set_Text("&Edit");
    // Add the menu items to the main menu.
    mainMenu1.get_MenuItems().Add(menuItem1);
    mainMenu1.get_MenuItems().Add(menuItem2);
    // Add functionality to the menu items. 
    menuItem1.add_Click(new System.EventHandler(this.menuItem1_Click));
    menuItem2.add_Click(new System.EventHandler(this.menuItem2_Click));
    // Assign mainMenu1 to the form.
    this.set_Menu(mainMenu1);
    // Perform a click on the File menu item.
    menuItem1.PerformClick();
} //CreateMyMenu

private void menuItem1_Click(Object sender, System.EventArgs e)
{
    MessageBox.Show("You clicked the File menu.", "The Event Information");
} //menuItem1_Click

private void menuItem2_Click(Object sender, System.EventArgs e)
{
    MessageBox.Show("You clicked the Edit menu.", "The Event Information");
} //menuItem2_Click

Windows 98, Windows Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft