c# - 禁用顶级菜单项不会禁用子项,尽管 MSDN 怎么说?

标签 c# .net winforms menu menuitem

根据 http://msdn.microsoft.com/en-us/library/aa984351%28VS.71%29.aspx

Disabling the first or toplevel menu item in a menu (for example, the "File" menu item in a traditional File menu) disables all the menu items contained within the menu. Likewise, disabling a menu item that has submenu items disables the submenu items.

根据 http://msdn.microsoft.com/en-us/library/ms171655.aspx

Disabling the first or top-level menu item in a menu disables all the menu items contained within the menu. Likewise, disabling a menu item that has submenu items disables the submenu items.

但是,如果我创建一个新的 Windows 窗体项目并添加以下代码,我仍然可以使用快捷键访问 Child 菜单项,根据 MSDN,应该 被禁用。

public Form1()
{
    InitializeComponent();

    // Main menu
    MenuStrip mainMenu = new MenuStrip();
    this.Controls.Add(mainMenu);

    // Top Level menu
    ToolStripMenuItem topLevelMenuItem = new ToolStripMenuItem("&Top Level");
    mainMenu.Items.Add(topLevelMenuItem);

    // Child menu item
    ToolStripMenuItem childMenuItem = new ToolStripMenuItem("&Child...", null, (o, e) => MessageBox.Show("Doing something."));
    childMenuItem.ShortcutKeys = Keys.Control | Keys.C;
    childMenuItem.ShortcutKeyDisplayString = "Ctrl + C";
    topLevelMenuItem.DropDownItems.Add(childMenuItem);

    // Menu item to toggle the Top Level menu's Enabled property
    mainMenu.Items.Add(new ToolStripMenuItem("Toggle Enable for Top Level menu", null, (o, e) =>
    {
        topLevelMenuItem.Enabled = !topLevelMenuItem.Enabled;
        MessageBox.Show("topLevelMenuItem.Enabled = " + topLevelMenuItem.Enabled + Environment.NewLine + "childMenuItem.Enabled = " + childMenuItem.Enabled);
    }));
}

我可以看到 childMenuItem.Enabled 根本没有改变,而 topLevelMenuItem.Enabled 改变了。

当然,我可以使用 for 循环来禁用 Top Level 菜单下的所有菜单项,或者甚至只禁用 Child 菜单项,但是根据 MSDN 我不应该。这是怎么回事?我是不是遗漏了什么、误解了什么,或者 MSDN 只是错了?

最佳答案

这是一个错误。 ToolStripItem 类中有很多错误,当时机未修复(.NET 2.0 发布后的某个时间),现在为时已晚。将这些错误发布到 Connect 反馈站点没有用,它们只会告诉您访问 MSDN 论坛以找到解决方法。我想你已经有了一个。首先,here's one符合您的情况。

关于c# - 禁用顶级菜单项不会禁用子项,尽管 MSDN 怎么说?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2178904/

相关文章:

c# - .Net (C#) Enum 重写为 java

c# - Invalidate()将东西留在屏幕上

c# - 转到文本编辑器中的行

C# - 遍历谓词的模式

c# - 复杂 Xamarin Forms 自定义控件中的内容

.net - 我什么时候应该使用 UdpClient.BeginReceive?什么时候应该在后台线程上使用 UdpClient.Receive?

java - 请推荐一个将 C# 应用程序移植到 Mac 的平台?

c# - 在两个文本框之间检查

c# - 以相反的顺序从字符串中获取值

c# - FileUpload 加载 .XML