This documentation is archived and is not being maintained.

MenuItem.PerformSelect Method

Raises the Select event for this menu item.

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

Remarks

This method allows you to raise the Select event without passing any event information to the event handler.

Example

[Visual Basic, C#, C++] In this example you programmatically select a menu item by using the PerformSelect method. First, you create a main menu (mainMenu1) and add to it two menu items, menuItem1 (File) and menuItem2 (Edit). You also use the Select event to send data to the event handler when a menu item is selected. Then you use the PerformSelect method to select the File menu item. When you start the application, the File menu item is selected, and a message box that contains the text "The File menu is selected." 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.Select, AddressOf Me.menuItem1_Select
    AddHandler menuItem2.Select, AddressOf Me.menuItem2_Select

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

    ' Select the File menu item.
    menuItem1.PerformSelect()
End Sub 'CreateMyMenu


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


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

[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.Select += new System.EventHandler(this.menuItem1_Select);
    menuItem2.Select += new System.EventHandler(this.menuItem2_Select);
    
    // Assign mainMenu1 to the form.
    this.Menu=mainMenu1;

    // Select the File menu item.
    menuItem1.PerformSelect();
}

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

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

[C++] 
public:
    void CreateMyMenu() {
        // Create a main menu objects.
        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->Select += new System::EventHandler(this, &Form1::menuItem1_Select);
        menuItem2->Select += new System::EventHandler(this, &Form1::menuItem2_Select);

        // Assign mainMenu1 to the form.
        this->Menu=mainMenu1;

        // Select the File menu item.
        menuItem1->PerformSelect();
    }

private:
    void menuItem1_Select(Object* /*sender*/, System::EventArgs* /*e*/) {
        MessageBox::Show(S"You selected the File menu.", S"The Event Information");
    }

    void menuItem2_Select(Object* /*sender*/, System::EventArgs* /*e*/) {
        MessageBox::Show(S"You selected 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 | PerformClick

Show: