8 Replies Latest reply on Jun 28, 2010 8:35 AM by jsd99

    enabling and disabling buttons in actionscript

    BosDog Level 1

      in flex i love using the enabled option for buttons (so when the user has not yet selected something in a datagrid or if a variable in the app is not properly set the button will not be enabled and will be grayed out... but once the condition is true the button automatically becomes enabled) like this:

       

      <mx:Button

       

       

      label="Save Changes" icon="@Embed(source='images/disk.png')" click="saveData();"

      enabled="

      {firstID != 0 &amp;&amp; changesMade == true}"/>

       

      i have a control bar that has buttons but if the user is an Admin they have additional buttons (which i created using actionscript in an initApp() function once the app initializes like this):

       

       

      if (adminList.indexOf(Application.application.parameters.emplid) != -1){

       

      var but:Button = new Button();

      but.label =

      "Delete Account";

      but.addEventListener(

      "click",confirm);

      but.setStyle(

      "icon", deleteIcon);

       

      this.toolbar.addChild(but);

      }

       

      how can i code the enabled function that i use i mxml in actionscript in the code above?

        • 1. Re: enabling and disabling buttons in actionscript
          VRPDeveloper Level 3

          You can create function which returns boolean and also create bindable variable and write logic into it and assign value to that variable .

           

          Then use but.enabled =functionname;

           

          may be it will help u

          • 2. Re: enabling and disabling buttons in actionscript
            BosDog Level 1

            i tried this but the app wont run due to an error in the flex app reading "return value must be undefined": here is my code

             

             

             

            public function loadAdmins(returnedEvent:ResultEvent):void {

            adminList = returnedEvent.result[0].DEPT_LIST;

             

            if (adminList.indexOf(Application.application.parameters.emplid) != -1){

             

            var delBtn:Button = new Button();

            delBtn.label =

            "Delete Account";

            delBtn.addEventListener(

            "click",confirm);

            delBtn.setStyle(

            "icon", deleteIcon);

            delBtn.enabled=delBtnEnabled();

             

            this.toolbar.addChild(delBtn);

            }

            }

             

             

            public function delBtnEnabled():void{

             

            var test:Boolean = false;

             

            return (test);

            }

             

            i also tried creating a bindable variable like this:

            [Bindable]

            public var delBtn:Button;

             

            and took out the var delBtn:Button = new Button();

             

            but when i do that the function never creates the button and gives me an error of a null value.

            • 3. Re: enabling and disabling buttons in actionscript
              VRPDeveloper Level 3

              Your function have void return tyoe make it as boolean like this

               

              [Bindable]

              public function delBtnEnabled():Boolean{

               

              //Here write code of using firmid ....On the basis of firmid , it will retunr ture or false ...

               

              return false;

              }

              • 4. Re: enabling and disabling buttons in actionscript
                BosDog Level 1

                oops i just realized i forgot the return type.

                 

                this is helpful when the button is first created and the app is first initialized... but i need this button to automatically become enabled if the user does something (like click on a datagrid) and automatically disabled when the user clicks off of the datagrid (or some action that would enable/disable the button).

                 

                 

                 

                public function loadAdmins(returnedEvent:ResultEvent):void {

                adminList = returnedEvent.result[0].DEPT_LIST;

                 

                if (adminList.indexOf(Application.application.parameters.emplid) != -1){

                 

                var delBtn:Button = new Button();

                delBtn.label =

                "Delete Account";

                delBtn.addEventListener(

                "click",confirm);

                delBtn.setStyle(

                "icon", deleteIcon);

                delBtn.enabled=delBtnEnabled();

                 

                this.toolbar.addChild(delBtn);

                }

                }

                 

                 

                public function delBtnEnabled():Boolean{

                 

                var returnVal:Boolean;

                 

                if (firstID != 0){

                returnVal =

                true;

                }

                 

                else{

                returnVal =

                false;

                }

                 

                return returnVal;

                }

                • 5. Re: enabling and disabling buttons in actionscript
                  VRPDeveloper Level 3

                  The other workaround is in this way

                   

                    BindingUtils.bindProperty(but,"enabled",tst,"value"); where tst is bindable variable whose value depen on ur firmid logic....

                   

                   

                  Hope now it will work!!!

                  • 6. Re: enabling and disabling buttons in actionscript
                    jsd99 Level 3

                    I would create all the buttons in MXML and then when the app starts up, remove the admin ones if the user isn't an admin.  Or you make them invisible in the MXML and set visible = true when the app starts if admin is true.

                     

                    This way you can keep using the MXML syntax which you are used to.

                    • 7. Re: enabling and disabling buttons in actionscript
                      BosDog Level 1

                      JSD thanks for that helpful insight! It is a lot easier to remove an existing button created in Flex with removeChild in actionscript than

                      it is to add one and bind it to something using actionscript.

                       

                      I don't like just toggling visibility b/c those components still take up "real estate" on the screen and throws the centering of the buttons off. removeChild is definitely the way to go!

                       

                      ps I've done something similar with states in the past... but wanted to move away from having to use states just for admin controls.

                      • 8. Re: enabling and disabling buttons in actionscript
                        jsd99 Level 3

                        BosDog wrote:

                         

                        JSD thanks for that helpful insight! It is a lot easier to remove an existing button created in Flex with removeChild in actionscript than

                        it is to add one and bind it to something using actionscript.

                         

                        I don't like just toggling visibility b/c those components still take up "real estate" on the screen and throws the centering of the buttons off. removeChild is definitely the way to go!

                         

                        ps I've done something similar with states in the past... but wanted to move away from having to use states just for admin controls.

                        cool, please mark my answer as helpful/correct

                         

                        as for taking up 'real estate' if you put them in a container like an HBox and use relative positioning to place the box, then removing the buttons will cause the box to shrink.  i use this trick all the time in my own apps.