Disabling top-level menu item does not disable children, in spite of what MSDN says?
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.En开发者_如何学JAVAabled = !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?
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.
精彩评论