尽管 MSDN 是这么说的,禁用顶级菜单项并不会禁用子菜单项?

发布于 2024-08-19 21:22:26 字数 1941 浏览 6 评论 0原文

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

禁用菜单中的第一个或顶级菜单项(例如,传统“文件”菜单中的“文件”菜单项)将禁用该菜单中包含的所有菜单项。同样,禁用具有子菜单项的菜单项也会禁用子菜单项。

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

禁用菜单中的第一个或顶级菜单项会禁用该菜单中包含的所有菜单项。同样,禁用具有子菜单项的菜单项也会禁用子菜单项。

但是,如果我创建一个新的 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 就是错误的?

According to 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.

According to 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.

However, if I create a new Windows Forms project and add the following code, I can still use the shortcut key to access the Child menu item that, according to MSDN, should be disabled.

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);
    }));
}

I can see that childMenuItem.Enabled is not changing at all, while topLevelMenuItem.Enabled does.

Sure, I could use a for loop to disable all menu items under the Top Level menu, or even disable just the Child menu item, but according to MSDN I shouldn't have to. What's the deal? Am I missing something, misinterpreting something, or is MSDN just wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨巷深深 2024-08-26 21:22:26

这是一个错误。 ToolStripItem 类中有很多错误,它们在适当的时候(.NET 2.0 发布后的某个时间)没有得到修复,现在为时已晚。将这些错误发布到 Connect 反馈站点没有任何用处,它们只会告诉您访问 MSDN 论坛以查找解决方法。我想你已经有了一个。 Fwiw, 这是与您的情况相符的

It is a bug. There are lots of bugs in the ToolStripItem classes, they were not fixed when the time was right (some time after the .NET 2.0 release), now it is too late. Posting these bugs to the Connect feedback site isn't useful, they'll just tell you to visit the MSDN forums to find a workaround. I think you already got one. Fwiw, here's one that matches your case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文