共用方式為


MenuItem.PerformClick 方法

定義

產生 ClickMenuItem 事件,模擬使用者按下的動作。

public:
 void PerformClick();
public void PerformClick ();
member this.PerformClick : unit -> unit
Public Sub PerformClick ()

範例

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

public:
   void CreateMyMenu()
   {
      // Create a main menu object.
      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->Click += gcnew System::EventHandler( this, &Form1::menuItem1_Click );
      menuItem2->Click += gcnew 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( "You clicked the File menu.", "The Event Information" );
   }

   void menuItem2_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      MessageBox::Show( "You clicked 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.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");		
}
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


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


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

備註

您可以使用此功能表透過程式碼啟動功能表項目,而不需傳遞任何事件資訊。 例如,如果您想要根據應用程式中發生的動作來啟動功能表項目,您可以針對該 MenuItem 呼叫 PerformClick 方法。

適用於