C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Example: Here are two methods that set menus to enabled or disabled. When Enabled is changed to false, the menu will turn gray.
PropertyNote: The methods must be called manually: we can call them from the code in an event handler.
Example code that disables menu: C#
void EnableFileToolsMenu(bool state)
{
foreach (ToolStripItem item in fileToolStripMenuItem.DropDownItems)
{
item.Enabled = state;
}
foreach (ToolStripItem item in toolsToolStripMenuItem.DropDownItems)
{
item.Enabled = state;
}
}
void EnableEditMenu(bool state)
{
foreach (ToolStripItem item in editToolStripMenuItem1.DropDownItems)
{
item.Enabled = state;
}
}
Tip: This code disables two menus at once. This adds to simplicity and efficiency in the GUI layer.
Tip: You can make menus equal to an expression, such as the Enabled property of a password box.
Example code that uses the above methods: C#
void passwordTextBox_EnabledChanged(object sender, EventArgs e)
{
bool state = passwordTextBox.Enabled;
EnableFileToolsMenu(!state);
}
void ExampleMethod()
{
// Enable all the items in these menus.
EnableFileToolsMenu(true);
// Disable the Edit menu.
EnableEditMenu(false);
}