MenuItem.Enabled 属性

定义

获取或设置一个值,通过该值指示菜单项是否启用。Gets or sets a value indicating whether the menu item is enabled.

public:
 property bool Enabled { bool get(); void set(bool value); };
public bool Enabled { get; set; }
member this.Enabled : bool with get, set
Public Property Enabled As Boolean

属性值

Boolean

如果启用菜单项,则为 true;否则为 falsetrue if the menu item is enabled; otherwise, false. 默认值为 trueThe default is true.

示例

下面的代码示例演示如何使用 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 窗体上的控件是否已启用、是否具有输入焦点,以及是否在启用对象之前选择了文本 MenuItemThe example determines if textBox1, a TextBox control on the form, is enabled, has input focus, and has text selected before enabling the MenuItem objects. 此示例需要创建 MenuItem 名为、和的三个对象 menuCut menuCopy menuDeleteThis example requires that three MenuItem objects are created named menuCut, menuCopy, and menuDelete have been created.

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;
      }
      else
      {
         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;
   }
   else
   {
      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
   Else
      menuCut.Enabled = True
      menuCopy.Enabled = True
      menuDelete.Enabled = True
   End If
End Sub

注解

禁用的将以 MenuItem 灰色显示,以指示其状态。A MenuItem that is disabled is displayed in a gray color to indicate its state. 禁用父菜单项时,不显示所有子菜单项。When a parent menu item is disabled, all submenu items are not displayed.

适用于