Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Toggling UI Elements (ToolStrip and MenuStrip Items and the Like)

Toggling UI Elements (ToolStrip and MenuStrip Items and the Like)

Scheduled Pinned Locked Moved C#
questiondesigndockertutorial
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Matt U
    wrote on last edited by
    #1

    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 selection

    TreeNode 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!

    J 1 Reply Last reply
    0
    • M Matt U

      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 selection

      TreeNode 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!

      J Offline
      J Offline
      Jimmanuel
      wrote on last edited by
      #2

      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 each TreeNode to store a List of ToolStripItems that should be enabled when that node is selected. When the nodes are created (or in the form constructor, whichever is applicable) initialize the Lists. Then in the selection change handler disable everything, then cycle through the list of ToolStripItems 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 selection

      TreeNode 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:

      M 2 Replies Last reply
      0
      • J Jimmanuel

        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 each TreeNode to store a List of ToolStripItems that should be enabled when that node is selected. When the nodes are created (or in the form constructor, whichever is applicable) initialize the Lists. Then in the selection change handler disable everything, then cycle through the list of ToolStripItems 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 selection

        TreeNode 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:

        M Offline
        M Offline
        Matt U
        wrote on last edited by
        #3

        Wow, thanks. LoL. I had no idea about the "Tag" property for TreeNode objects. :-) I'll play with the example you gave and if I need anymore information I'll come back. Thanks a lot. :-D

        1 Reply Last reply
        0
        • J Jimmanuel

          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 each TreeNode to store a List of ToolStripItems that should be enabled when that node is selected. When the nodes are created (or in the form constructor, whichever is applicable) initialize the Lists. Then in the selection change handler disable everything, then cycle through the list of ToolStripItems 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 selection

          TreeNode 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:

          M Offline
          M Offline
          Matt U
          wrote on last edited by
          #4

          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. :-)

          J 1 Reply Last reply
          0
          • M Matt U

            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. :-)

            J Offline
            J Offline
            Jimmanuel
            wrote on last edited by
            #5

            Glad to help :)

            :badger:

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups