9 Replies Latest reply on Jul 3, 2008 1:53 PM by VarioPegged

    Enable / Disable Checkbox

    Appleseed27
      I am writing a flex application that will use check boxes that will be enabled under certain conditions. Right now I have it halfway working with a CLICK MouseEvent, but by doing it this way the conditions are only checked when the checkbox is clicked. Is there a method or property of the checkbox that will check the conditions in realtime, and not with a MouseEvent?
        • 1. Re: Enable / Disable Checkbox
          matthew horn Level 3
          You will probably want to use data binding. Let's assume you have some method that gets called when the "conditions" changes. In that method, you'd set the value of a Boolean to either true or false. You'd then bind that value to the checkbox's enabled property.

          Sounds harder than it is. An example will probably be best. This example also uses a click event, but on a different component (the button). What it shows you is that you can set the value of a var called stateOfCheckBox from anywhere in your app, and you will be able to toggle the state of the checkbox.

          hth,
          matt horn
          flex docs

          <?xml version="1.0"?>
          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" creationComplete="initApp();" >
          <mx:Script>
          <![CDATA[
          [Bindable]
          private var stateOfCheckBox:Boolean;

          private function initApp():void {
          stateOfCheckBox = true; //default to true
          }

          private function changeStateOfCheckBox(e:Event):void {
          if (myCheckBox.enabled) {
          stateOfCheckBox = false;
          } else {
          stateOfCheckBox = true;
          }
          }
          ]]>
          </mx:Script>
          <mx:CheckBox id="myCheckBox" enabled="{stateOfCheckBox}"/>
          <mx:Button id="myButton" click="changeStateOfCheckBox(event)" label="Toggle Checkbox"/>
          </mx:Application>
          • 2. Enable / Disable Checkbox
            Appleseed27 Level 1
            To be more specific, lets say I have a checkbox (A) that only enables when a HSlider (B) has a value of 10 and another checkbox (C) is selected.

            In my script I have:

            if (C.selected==true)
            {
            if(B.value>10)
            {
            A.enabled=true;
            }
            }

            else
            {
            A.selected=false;
            A.enabled=false;
            }

            At the moment I have this attached to checkbox (C)'s click property. So the problem is I have to change HSlider (B)'s value before I select C. I would like it to be that I can changed B and C's values in any order and get checkbox A to enable. Is that what the [Bindable] in post above does? Also, I would like the method to be attached to the checkbox being enabled (C). Is there a property that I can attach the method too?
            • 3. Re: Enable / Disable Checkbox
              Gregory Lafrance Level 6
              I would respond to the change event.
              • 4. Re: Enable / Disable Checkbox
                Appleseed27 Level 1
                I think the event change is what I am looking for. I tried implementing the event, but I am getting an Actionscript error. Below is a modified version of the Actionscript (control objects are generic, EX: Checkbox2). I used the code from http://livedocs.adobe.com/flex/3/html/help.html?content=components_06.html to come up with the code I have. The initApp() is called as creationComplete="initApp();" at the beginning of the main program.

                ActionScript Error: TypeError: Error #1009: Cannot access a property or method of a null object reference.
                at main/initApp()[C:\flex3tfs\Character Creator\src\Scripts.as:42]
                at main/___main_WindowedApplication1_creationComplete()[C:\flex3tfs\Character Creator\src\main.mxml:3]
                at flash.events::EventDispatcher/dispatchEventFunction()
                at flash.events::EventDispatcher/dispatchEvent()
                .....



                • 5. Re: Enable / Disable Checkbox
                  Gregory Lafrance Level 6
                  One thing you should do is have Checkbox2Enabler as the change event listener for the checkbox and the hslider.

                  If your controls are inside a ViewStack, TabNavigator, etc. and are not in the initially displayed screen, you might get the null reference error.

                  If you can boil your case down to a condensed yet complete sample code showing the null reference issue we can help.
                  • 6. Re: Enable / Disable Checkbox
                    Appleseed27 Level 1
                    Here is the sample code of what I am trying to do. I couldn't get the Actionscript error to show up this code, but the checkbox isn't enabling anyway. Is there something I'm missing in the implementation?

                    • 7. Re: Enable / Disable Checkbox
                      Gregory Lafrance Level 6
                      Like I said, you need to add the event listener for the HSlider too, though I think your logic should have used an &&, as I did in this code that works as desired:
                      • 8. Enable / Disable Checkbox
                        Appleseed27 Level 1
                        Ok, I think I see where I am getting the error in my main program. If I put the checkbox and slider on the same canvas then it works fine. If I put either the checkbox or the slider on different canvas then I get an actionscript error. Has anyone seen this error before?

                        • 9. Re: Enable / Disable Checkbox
                          VarioPegged Level 2
                          Set your ViewStack's creationPolicy="all". Because the ViewStack uses deferred instantiation by default (children are only instantiated when a user selects a specific view). So, the instances have not yet been created by the time you're trying to set the event listener for Checkbox1.

                          TS