MenuItem.PerformSelect Method
Assembly: System.Windows.Forms (in system.windows.forms.dll)
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 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.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"); }
/** @attribute STAThread()
*/
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.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_Select(new System.EventHandler(this.menuItem1_Select));
menuItem2.add_Select(new System.EventHandler(this.menuItem2_Select));
// Assign mainMenu1 to the form.
this.set_Menu(mainMenu1);
// Select the File menu item.
menuItem1.PerformSelect();
} //CreateMyMenu
private void menuItem1_Select(Object sender, System.EventArgs e)
{
MessageBox.Show("You selected the File menu.", "The Event Information");
} //menuItem1_Select
private void menuItem2_Select(Object sender, System.EventArgs e)
{
MessageBox.Show("You selected the Edit menu.", "The Event Information");
} //menuItem2_Select
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.