MenuItem.PerformSelect 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
引发该菜单项的 Select 事件。
public:
virtual void PerformSelect();
public virtual void PerformSelect ();
abstract member PerformSelect : unit -> unit
override this.PerformSelect : unit -> unit
Public Overridable Sub PerformSelect ()
示例
在此示例中,你将使用 PerformSelect
方法以编程方式选择菜单项。 首先,创建main菜单 (mainMenu1
) ,并向其添加两个菜单项, menuItem1
(File
) 和menuItem2
(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 而无需将任何事件信息传递给事件处理程序。