14 Replies Latest reply on Feb 15, 2012 2:18 AM by CKPTmartin

    Enable or disable menu items

    Pigg_Man Level 1
      I'm trying to disable menu items in the menu bar... In the past I used this code and it doesn't work:
      var Item = MyMenuBar.getMenuAt(0);
      Item.setMenuItemEnabled(Item.getMenuItemAt(2), false);

      I now have this code.. but the menuItem doesn't have a setMenuItemEnabled state...
      var menuItem:Menu = MyMenuBar.getMenuAt(posMenuBar);

      Any clue how to set it to disabled
        • 1. Re: Enable or disable menu items
          dimival Level 1
          Just checked the documentation and the MenuBar has a menubarItems property which is the array of MenuBarItems of the Menu, now MenuBarItem does have a enabled property so i think you could do this:

          menuBar.menubarItems[0].enabled = false;

          Try it
          • 2. Re: Enable or disable menu items
            Pigg_Man Level 1
            menuBar.menubarItems[0].enabled = false;

            this produces a compile time error
            • 3. Re: Enable or disable menu items
              Pigg_Man Level 1
              enabled isn't a property off of the menu bar control
              • 4. Enable or disable menu items
                dimival Level 1
                Do you have MenuBarItems? because if you have none, the property menubarItems will contain an empty array. Make sure menubarItems does contain items
                • 5. Re: Enable or disable menu items
                  Pigg_Man Level 1
                  this is my xml collection that i'm binding to the menubar dataprovider:

                  <mx:XMLListCollection id="MyMenuBarDP">
                  <mx:XMLList>
                  <node label="1">
                  <node label="1"/>
                  <node label="2"/>
                  <node label="3"/>
                  </node>
                  <node label="2">
                  <node label="1"/>
                  <node label="2"/>
                  <node label="3"/>
                  </node>
                  </mx:XMLList>
                  </mx:XMLListCollection>

                  this line of code grabs the first node and it's children:
                  menuBar.menubarItems[0]

                  I need to grab the node and one of it's child (single node) and then disable it ... not the head node
                  • 6. Re: Enable or disable menu items
                    Pigg_Man Level 1
                    in flax mx the code to do this is

                    var Item = MyMenuBar.getMenuAt(posMenuBar);
                    Item.setMenuItemEnabled(Item.getMenuItemAt(posSubItem), bool);
                    • 7. Re: Enable or disable menu items
                      ntsiii Level 3
                      I think you will want:
                      menuBar.node[0].@enabled = false;

                      enabled is an attribute, not a property

                      Tracy
                      • 8. Re: Enable or disable menu items
                        Pigg_Man Level 1
                        I tried this and I get a compile error:

                        var myMenu:Menu = MyMenuBar.getMenuAt (0);
                        var item:DisplayObject = myMenu.getChildAt (1);
                        item.@enabled = false;
                        • 9. Re: Enable or disable menu items
                          Pigg_Man Level 1
                          I tried this and I get a compile error:

                          var myMenu:Menu = MyMenuBar.getMenuAt (0);
                          var item:DisplayObject = myMenu.getChildAt (1);
                          item.@enabled = false;

                          the first two lines of code return the first menu of the menubar and then the second line of code returns the item of the menu... I'm trying to set enable = false on the menu item
                          • 10. Re: Enable or disable menu items
                            ntsiii Level 3
                            You are mixing types. @enabled is not valid for a DisplayObject. The item you want to enable is the XML node.

                            Go straight to the MyMenuBarDP xmllist, and use e4x to get to the node you want, and set @enabled on that

                            Tracy
                            • 11. Re: Enable or disable menu items
                              Pigg_Man Level 1
                              Thanks a bunch... heres what I did.. I made a function:

                              private function enableSubMenuItem (posMenuBar:int, posSubItem:int, bool:Boolean) : void {
                              var menu:XML = XML (MyMenuBarDP.getItemAt(posMenuBar));
                              menu.node[posSubItem].@enabled = bool;
                              }

                              and then used these lines of code:
                              enableSubMenuItem(0, 0, true);
                              or
                              enableSubMenuItem(0, 0, false);

                              that way you can enable or disable items throughout you menubar structure...

                              I took the XMLListCollection that my menu bar was using and converted it to XML and then used E4x to access the enable attribute... Talk about a headache :)

                              How did you find out that "enable" was an attribute of the XML object?
                              • 12. Re: Enable or disable menu items
                                ntsiii Level 3
                                I had just started working with menus and found and copied this example:
                                http://livedocs.macromedia.com/flex/2/docs/00000579.html

                                It shows the enabled attribute and also how to do a check and radio item.

                                Tracy
                                • 13. Re: Enable or disable menu items
                                  kris456

                                  I have a MenuBar Control which has a XMLList as the data provider.

                                   

                                  The XML List is:

                                  private var menubarXML:XMLList =
                                                  <>
                                                      <menuitem label="Menu1" data="top">
                                                          <menuitem label="MenuItem 1-A" data="1A"/>
                                                          <menuitem label="MenuItem 1-B" data="1B"/>
                                                      </menuitem>
                                                      <menuitem label="Menu2" data="top">
                                                          <menuitem label="MenuItem 2-A" type="check"  data="2A"/>
                                                          <menuitem type="separator"/>
                                                          <menuitem label="MenuItem 2-B" >
                                                              <menuitem label="SubMenuItem 3-A" type="radio" 
                                                                  groupName="one" data="3A" toggled="true"/>
                                                              <menuitem label="SubMenuItem 3-B" type="radio"
                                                                  groupName="one" data="3B"/>
                                                          </menuitem>
                                                      </menuitem>
                                                  </>;
                                  

                                   

                                  The Menu bar control is

                                   <mx:MenuBar id="men" labelField="@label" itemClick="menuHandler(event);" 
                                              dataProvider="{menuBarCollection}"  width="100%" />
                                                          
                                  
                                  

                                   

                                   

                                  I wanted to disable a submenu Item in on of the menu when user clicks button. So inorder to achieve it I created a function

                                   

                                   private function enableSubMenuItem (posMenuBar:int, posSubItem:int, bool:Boolean) : void 
                                           {
                                              var menu:XML = XML (menuBarCollection.getItemAt(posMenuBar));
                                              menu.node[posSubItem].@enabled = bool;
                                           }

                                   

                                   

                                  Then I called the function enableSubMenuItem in the function which is invoked when the button is clicked by

                                   

                                  enableSubMenuItem(0, 1, true);
                                  

                                   

                                  But when I click I get the following sandbox error at the line :

                                   

                                  menu.node[posSubItem].@enabled = bool; (in the function  enableSubMenuItem)

                                   

                                  TypeError: Error #1010: A term is undefined and has no properties.

                                   

                                  Why could this be so ?

                                  Please Help me resolve the issue.

                                  • 14. Re: Enable or disable menu items
                                    CKPTmartin

                                    I know this is way old. But, I just stumbled across this and wanted to post the solution to kris456 problem.

                                    The described functionality works just fine. You only have to use the node names as found in your XML List.

                                     

                                    In your case you should have been using

                                     

                                    menu.menuitem[posSubItem].@enabled = bool;

                                     

                                    instead of "menu.node", since your XML doesn't use the name "node" for the XML nodes.