MenuItem.PerformSelect メソッド

定義

このメニュー項目に Select イベントを発生させます。Raises the Select event for this menu item.

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

この例では、 PerformSelectメソッドを使用して、プログラムでメニュー項目を選択します。In this example you programmatically select a menu item by using the PerformSelect method. まず、メインメニュー (mainMenu1) を作成し、それに2つFilemenuItem1メニュー項目 () とmenuItem2 (Edit) を追加します。First, you create a main menu (mainMenu1) and add to it two menu items, menuItem1 (File) and menuItem2 (Edit). また、 Selectイベントを使用して、メニュー項目が選択されたときにイベントハンドラーにデータを送信することもできます。You also use the Select event to send data to the event handler when a menu item is selected. 次に、 PerformSelectメソッドを使用してFileメニュー項目を選択します。Then you use the PerformSelect method to select the File menu item. アプリケーションをFile起動すると、メニュー項目が選択され、"ファイルメニューが選択されています" というテキストを含むメッセージボックスが表示されます。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. この例では、という名前Form Form1のを作成しておく必要があります。The example requires that you have created a Form named 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イベントハンドラーにイベント情報を渡さずにイベントを発生させることができます。This method allows you to raise the Select event without passing any event information to the event handler.

適用対象

こちらもご覧ください