4 Replies Latest reply on Apr 26, 2010 10:53 PM by David_F57

    Passing a control name to a function

    mumcs01 Level 1

      I"m trying to make a reusable function that will take a checkbox event form one form item and enable or disable another option. In this small example I'm using checkbox: db to try and enable or disable the db_size field. So the function I came up with I want to pass the event, and then the name of the field I want to effect. (db_size in this case). How do I pass the db_size. Below doesn't work. It seems I need to 'type' the _function name t something, but to a String doesn't work, or to a Class? Any ideas. I'm sure this is possible.

       

       

      private function hidden_field_validator(event:Event, _function:Class):void
      {
            if (event.currentTarget.toggle==true) {
            _function.enabled=true;
      }
      else {
            _function.enabled=false;
      }
      }
       
      ]]>
      </mx:Script>

      <mx:Label text="Database Logging"/>

      <mx:FormItem label="Database server enabled at Boot:">
      <mx:CheckBox id="db" change="hidden_field_validator(event, db_size)" />
      </mx:FormItem>

       

      <mx:FormItem label="Logging Database Size Limit (GB):">
      <mx:NumericStepper id="db_size" minimum="5" maximum="250"
        stepSize="1" change="parentDocument.addavalue('db_size',db_size.value);"/>
      </mx:FormItem>

        • 1. Re: Passing a control name to a function
          Flex harUI Adobe Employee

          A Class is a template for instances which are all Objects.

           

          private function hidden_field_validator(event:Event, instance:Object):void

          {

                if (event.currentTarget.toggle==true) {

                instance.enabled=true;

          }

          else {

                instance.enabled=false;

          }

          }

          1 person found this helpful
          • 2. Re: Passing a control name to a function
            David_F57 Level 5

            hi,

             

            there is a much easier way to do this using binding

             

             

            <mx:CheckBox id="chkBox" x="416" y="500" label="CheckBox"/>

            <mx:NumericStepper x="551" y="500" enabled="{chkBox.selected}"/>

             

             

            The stepper enabled property is directly connected to the checkbox selected property.

             

            David.

            • 3. Re: Passing a control name to a function
              mumcs01 Level 1

              Wow. The binding method is so COOL! Thanks! (Though knowing how to pass a function is really helpful)!

              • 4. Re: Passing a control name to a function
                David_F57 Level 5

                hi,

                 

                Thats the beauty of forums, you usually get an answer to your problem plus alternative solutions ( btw credit to answer really goes to Alex who as usually provided a succinct response to the question).

                 

                If you liked the binding method, you should love flex 4's two way binding

                 

                <mx:CheckBox id="chkBox" x="416" y="500" label="CheckBox"/>

                <mx:NumericStepper id="ns1" x="551" y="500" enabled="@{chkBox.selected}"/>

                 

                with just that little @ you now have 2 way control for instance in code if i type

                 

                ns1.enabled=false,
                the checkbox will become unchecked. this functionality is one of my favorite additions to flex.
                David.