MenuItem.PerformClick 方法

定义

Click 生成一个 MenuItem 事件,模拟用户的单击操作。

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

示例

在此示例中,使用 PerformClick 方法以编程方式单击菜单项。 首先,创建main菜单 (mainMenu1) ,并向其添加两个菜单项, menuItem1 (File) 和menuItem2 (Edit) 。 单击菜单项时, Click 还可以使用 事件将数据发送到事件处理程序。 PerformClick然后使用 方法单击File菜单项。 启动应用程序时, File 菜单项将激活,屏幕上会显示一个包含文本“单击文件菜单”的消息框。 该示例要求你已创建名为 FormForm1

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 方法。

适用于