MenuItem.OnPopup(EventArgs) 方法


引发 Popup 事件。Raises the Popup event.

 virtual void OnPopup(EventArgs ^ e);
protected virtual void OnPopup (EventArgs e);
abstract member OnPopup : EventArgs -> unit
override this.OnPopup : EventArgs -> unit
Protected Overridable Sub OnPopup (e As EventArgs)



包含事件数据的 EventArgsAn EventArgs that contains the event data.


下面的代码示例演示如何使用 Popup 事件来确定是否在显示为剪切、复制和删除操作提供支持的 MenuItem 对象之前是否启用它们。The following code example demonstrates how to use the Popup event to determine whether MenuItem objects that provide support for cut, copy, and delete operations are enabled before the menu they are displayed in is shown. 该示例确定是否 textBox1、窗体上的 TextBox 控件是否已启用、是否具有输入焦点,以及是否在启用 MenuItem 对象之前选择了文本。The example determines if textBox1, a TextBox control on the form, is enabled, has input focus, and has text selected before enabling the MenuItem objects. 此示例要求创建了三个名为 menuCutmenuCopymenuDeleteMenuItem 对象。This example requires that three MenuItem objects are created named menuCut, menuCopy, and menuDelete have been created.

   void PopupMyMenu( Object^ /*sender*/, System::EventArgs^ /*e*/ )
      if ( textBox1->Enabled == false || textBox1->Focused == false || textBox1->SelectedText->Length == 0 )
         menuCut->Enabled = false;
         menuCopy->Enabled = false;
         menuDelete->Enabled = false;
         menuCut->Enabled = true;
         menuCopy->Enabled = true;
         menuDelete->Enabled = true;
private void PopupMyMenu(object sender, System.EventArgs e)
   if (textBox1.Enabled == false || textBox1.Focused == false ||
      textBox1.SelectedText.Length == 0)
      menuCut.Enabled = false;
      menuCopy.Enabled = false;
      menuDelete.Enabled = false;
      menuCut.Enabled = true;
      menuCopy.Enabled = true;
      menuDelete.Enabled = true;
Private Sub PopupMyMenu(ByVal sender As Object, ByVal e As System.EventArgs) Handles menuEdit.Popup
   If textBox1.Enabled = False OrElse textBox1.Focused = False OrElse textBox1.SelectedText.Length = 0 Then
      menuCut.Enabled = False
      menuCopy.Enabled = False
      menuDelete.Enabled = False
      menuCut.Enabled = True
      menuCopy.Enabled = True
      menuDelete.Enabled = True
   End If
End Sub


引发事件时,将通过委托调用事件处理程序。Raising an event invokes the event handler through a delegate. 有关详细信息,请参阅处理和引发事件For more information, see Handling and Raising Events.


当在派生类中重写 OnPopup(EventArgs) 时,请确保调用基类的 OnPopup(EventArgs) 方法。When overriding OnPopup(EventArgs) in a derived class, be sure to call the base class's OnPopup(EventArgs) method.