Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

MenuItem.PerformClick Method

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

[Visual Basic]
Public Sub PerformClick()
[C#]
public void PerformClick();
[C++]
public: void PerformClick();
[JScript]
public function PerformClick();

Remarks

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.

Example

[Visual Basic, C#, C++] 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 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 menuItem1 As New MenuItem()
    Dim menuItem2 As 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. 
    AddHandler menuItem1.Click, AddressOf Me.menuItem1_Click
    AddHandler menuItem2.Click, AddressOf Me.menuItem2_Click

    ' Assign mainMenu1 to the form.
    Me.Menu = mainMenu1

    ' Perform a click on the File menu item.
    menuItem1.PerformClick()
End Sub 'CreateMyMenu


Private Sub menuItem1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    MessageBox.Show("You clicked the File menu.", "The Event Information")
End Sub 'menuItem1_Click


Private Sub menuItem2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    MessageBox.Show("You clicked the Edit menu.", "The Event Information")
End Sub 'menuItem2_Click

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

[C++] 
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 = S"&File";
        menuItem2->Text = S"&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, &Form1::menuItem1_Click);
        menuItem2->Click += new System::EventHandler(this, &Form1::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(S"You clicked the File menu.", S"The Event Information");
    }

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

[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:
© 2015 Microsoft