如何:禁用 ToolStripMenuItemHow to: Disable ToolStripMenuItems

你可以通过启用和禁用菜单项来限制或放宽用户在响应用户活动时所能执行的命令。You can limit or broaden the commands a user may make by enabling and disabling menu items in response to user activities. 默认情况下, 菜单项在创建时处于启用状态, 但可以通过Enabled属性进行调整。Menu items are enabled by default when they are created, but this can be adjusted through the Enabled property. 可以在设计时在 "属性" 窗口中或通过编程方式在代码中设置此属性。You can manipulate this property at design time in the Properties window or programmatically by setting it in code.

以编程方式禁用菜单项To disable a menu item programmatically

  • 在设置菜单项属性的方法中, 添加要将属性设置为Enabled false的代码。Within the method where you set the properties of the menu item, add code to set the Enabled property to false.

    MenuItem1.Enabled = False  
    
    menuItem1.Enabled = false;  
    
    menuItem1->Enabled = false;  
    

    提示

    禁用菜单中的第一个或顶级菜单项将隐藏菜单中包含的所有菜单项, 但不会将其禁用。Disabling the first or top-level menu item in a menu hides all the menu items contained within the menu, but does not disable them. 同样, 禁用具有子菜单项的菜单项将隐藏子菜单项, 但不会禁用子菜单项。Likewise, disabling a menu item that has submenu items hides the submenu items, but does not disable them. 如果给定菜单上的所有命令均不可供用户使用, 则会被视为良好的编程做法, 隐藏和禁用整个菜单, 因为这会显示一个干净的用户界面。If all the commands on a given menu are unavailable to the user, it is considered good programming practice to both hide and disable the entire menu, as this presents a clean user interface. 你应隐藏和禁用菜单, 并禁用菜单中的每个项和子菜单项, 因为仅限隐藏不会阻止通过快捷键访问菜单命令。You should hide and disable the menu, and disable every item and submenu item in the menu, because hiding alone does not prevent access to a menu command via a shortcut key. 将顶级Visible菜单项的属性设置为false以隐藏整个菜单。Set the Visible property of a top-level menu item to false to hide the entire menu.

请参阅See also