Toggling UI Elements (ToolStrip and MenuStrip Items and the Like)
-
Hi, fellow CodeProject members. :-) I'm writing an application that contains a menu strip and a tool strip. The main application window is an MDI Container and on the left-hand side of the window is a splitter with a TreeView control. Depending on the TreeView's selected node I would like to enable/disable certain tool strip and menu strip items. What is the best way to go about doing so? I currently do something like this:
private void mainTreeView_AfterSelect(object sender, TreeViewEventArgs e)
{
if (mainTreeView.SelectedNode == null) return; // No selectionTreeNode selectedNode = mainTreeView.SelectedNode; switch (selectedNode.Level) { case 0: // Root node // Toggle the 'Enabled' state of eight items total (combination of ToolStrip and MenuStrip items) // ... break; case 1: // Child node (only goes Root->Child at the moment; may expand allowed depth later on but not right now) if (selectedNode.Parent.Name == "RootNode1") { // Toggle items based on child of RootNode1 // ... } else if (selectedNode.Parent.Name == "RootNode2") { // Toggle items differently from RootNode1 // ... } break; }
}
}There are currently eight items total for which I would like to toggle depending on the TreeView selection. I am only wanting to toggle the 'Enabled' state for the items. That's because, for example, root node 1 is selected so the user can only select "New Item...". Or if a child of root node 1 is selected then the user can choose "New Item...", "Edit Item..." or "Delete Item...". And root node 2 holds a different type of item so root node 1 items shouldn't be enabled in that case. Is there a different way to go about this altogether? Do I have it all (or partially) incorrect? Or should I simply create a method for each case, such as "void ToggleRootNodeItems(int OneOrTwo)" and "void ToggleChildNodeItems(int ParentIsRootOneOrTwo)"? I hope I explained my question fully. If you need more information I can provide you with whatever you need. Thanks much!
-
Hi, fellow CodeProject members. :-) I'm writing an application that contains a menu strip and a tool strip. The main application window is an MDI Container and on the left-hand side of the window is a splitter with a TreeView control. Depending on the TreeView's selected node I would like to enable/disable certain tool strip and menu strip items. What is the best way to go about doing so? I currently do something like this:
private void mainTreeView_AfterSelect(object sender, TreeViewEventArgs e)
{
if (mainTreeView.SelectedNode == null) return; // No selectionTreeNode selectedNode = mainTreeView.SelectedNode; switch (selectedNode.Level) { case 0: // Root node // Toggle the 'Enabled' state of eight items total (combination of ToolStrip and MenuStrip items) // ... break; case 1: // Child node (only goes Root->Child at the moment; may expand allowed depth later on but not right now) if (selectedNode.Parent.Name == "RootNode1") { // Toggle items based on child of RootNode1 // ... } else if (selectedNode.Parent.Name == "RootNode2") { // Toggle items differently from RootNode1 // ... } break; }
}
}There are currently eight items total for which I would like to toggle depending on the TreeView selection. I am only wanting to toggle the 'Enabled' state for the items. That's because, for example, root node 1 is selected so the user can only select "New Item...". Or if a child of root node 1 is selected then the user can choose "New Item...", "Edit Item..." or "Delete Item...". And root node 2 holds a different type of item so root node 1 items shouldn't be enabled in that case. Is there a different way to go about this altogether? Do I have it all (or partially) incorrect? Or should I simply create a method for each case, such as "void ToggleRootNodeItems(int OneOrTwo)" and "void ToggleChildNodeItems(int ParentIsRootOneOrTwo)"? I hope I explained my question fully. If you need more information I can provide you with whatever you need. Thanks much!
Using a switch or creating a different method for each node are valid ways to do it. My preferred method in this case would be to use the
Tag
property of eachTreeNode
to store aList
ofToolStripItem
s that should be enabled when that node is selected. When the nodes are created (or in the form constructor, whichever is applicable) initialize theList
s. Then in the selection change handler disable everything, then cycle through the list ofToolStripItem
s for the selected node and re-enable each one. Like this:internal MyConstructor()
{
// init the list of items for root node 1
List<ToolStripItem> items = new List<ToolStripItem>();
items.Add(newItemButton);
items.Add(editButton);
// add the rest of the items that should be enabled
rootNode1.Tag = items;// init the list of items for root node 2 items = new List<ToolStripItem>(); items.Add(deleteItemButton); items.Add(crashTheComputerButton); // add the rest of the items that should be enabled rootNode2.Tag = items; // init the list of items for etc // . . .
}
private void mainTreeView_AfterSelect(object sender, TreeViewEventArgs e)
{
if (mainTreeView.SelectedNode == null) return; // No selectionTreeNode selectedNode = mainTreeView.SelectedNode; foreach(ToolStripItem item in /\* the entire collection of toggle-able ToolStripItems \*/) { item.Enabled = false; } List<ToolStripItem> applicableItems = selectedNode.Tag as List<ToolStripItem>; if (applicableItems == null) { return; } foreach(ToolStripItem item in applicableItems) { item.Enabled = true; }
}
:badger:
-
Using a switch or creating a different method for each node are valid ways to do it. My preferred method in this case would be to use the
Tag
property of eachTreeNode
to store aList
ofToolStripItem
s that should be enabled when that node is selected. When the nodes are created (or in the form constructor, whichever is applicable) initialize theList
s. Then in the selection change handler disable everything, then cycle through the list ofToolStripItem
s for the selected node and re-enable each one. Like this:internal MyConstructor()
{
// init the list of items for root node 1
List<ToolStripItem> items = new List<ToolStripItem>();
items.Add(newItemButton);
items.Add(editButton);
// add the rest of the items that should be enabled
rootNode1.Tag = items;// init the list of items for root node 2 items = new List<ToolStripItem>(); items.Add(deleteItemButton); items.Add(crashTheComputerButton); // add the rest of the items that should be enabled rootNode2.Tag = items; // init the list of items for etc // . . .
}
private void mainTreeView_AfterSelect(object sender, TreeViewEventArgs e)
{
if (mainTreeView.SelectedNode == null) return; // No selectionTreeNode selectedNode = mainTreeView.SelectedNode; foreach(ToolStripItem item in /\* the entire collection of toggle-able ToolStripItems \*/) { item.Enabled = false; } List<ToolStripItem> applicableItems = selectedNode.Tag as List<ToolStripItem>; if (applicableItems == null) { return; } foreach(ToolStripItem item in applicableItems) { item.Enabled = true; }
}
:badger:
-
Using a switch or creating a different method for each node are valid ways to do it. My preferred method in this case would be to use the
Tag
property of eachTreeNode
to store aList
ofToolStripItem
s that should be enabled when that node is selected. When the nodes are created (or in the form constructor, whichever is applicable) initialize theList
s. Then in the selection change handler disable everything, then cycle through the list ofToolStripItem
s for the selected node and re-enable each one. Like this:internal MyConstructor()
{
// init the list of items for root node 1
List<ToolStripItem> items = new List<ToolStripItem>();
items.Add(newItemButton);
items.Add(editButton);
// add the rest of the items that should be enabled
rootNode1.Tag = items;// init the list of items for root node 2 items = new List<ToolStripItem>(); items.Add(deleteItemButton); items.Add(crashTheComputerButton); // add the rest of the items that should be enabled rootNode2.Tag = items; // init the list of items for etc // . . .
}
private void mainTreeView_AfterSelect(object sender, TreeViewEventArgs e)
{
if (mainTreeView.SelectedNode == null) return; // No selectionTreeNode selectedNode = mainTreeView.SelectedNode; foreach(ToolStripItem item in /\* the entire collection of toggle-able ToolStripItems \*/) { item.Enabled = false; } List<ToolStripItem> applicableItems = selectedNode.Tag as List<ToolStripItem>; if (applicableItems == null) { return; } foreach(ToolStripItem item in applicableItems) { item.Enabled = true; }
}
:badger:
Alright, after reading your post and researching this a little bit, I finally implemented the code. I'll tell you, it looks 10x cleaner. LoL. Another thing I noticed is that when I ran the Code Metrics (VS2010) before, the "Maintainability Index" for that particular form was around 46 or 48 I believe. After changing to the method you described it went up to 55. I still have a few areas that I need to break into separate methods, which I believe may bring it up some more. Thanks a lot for the help. :-)
-
Alright, after reading your post and researching this a little bit, I finally implemented the code. I'll tell you, it looks 10x cleaner. LoL. Another thing I noticed is that when I ran the Code Metrics (VS2010) before, the "Maintainability Index" for that particular form was around 46 or 48 I believe. After changing to the method you described it went up to 55. I still have a few areas that I need to break into separate methods, which I believe may bring it up some more. Thanks a lot for the help. :-)