根据 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/