共用方式為


MenuItem.PerformSelect 方法

定義

引發這個功能表項目的 Select 事件。

public:
 virtual void PerformSelect();
public virtual void PerformSelect ();
abstract member PerformSelect : unit -> unit
override this.PerformSelect : unit -> unit
Public Overridable Sub PerformSelect ()

範例

在此範例中,您會使用 PerformSelect 方法來以程式設計方式選取功能表項目。 首先,您會建立主功能表 (mainMenu1) ,並將它新增至兩個功能表項目, menuItem1 () FilemenuItem2 (Edit) 。 您也可以在選取功能表項目時, Select 使用 事件將資料傳送至事件處理常式。 PerformSelect然後使用 方法來選取 File 功能表項目。 當您啟動應用程式時, File 會選取功能表項目,並顯示一個訊息方塊,其中包含 [已選取檔案] 功能表的文字。 此範例會要求您已建立 Form 名為 的 Form1

public:
   void CreateMyMenu()
   {
      // Create a main menu objects.
      MainMenu^ mainMenu1 = gcnew MainMenu;

      // Create empty menu item objects.
      MenuItem^ menuItem1 = gcnew MenuItem;
      MenuItem^ menuItem2 = gcnew 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 += gcnew System::EventHandler( this, &Form1::menuItem1_Select );
      menuItem2->Select += gcnew 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( "You selected the File menu.", "The Event Information" );
   }

   void menuItem2_Select( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      MessageBox::Show( "You selected the Edit menu.", "The Event Information" );
   }
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");		
}
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


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


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

備註

這個方法可讓您引發事件, Select 而不需要將任何事件資訊傳遞至事件處理常式。

適用於

另請參閱