2 Replies Latest reply on Jul 13, 2009 12:41 PM by lee704

    Enable button based on ComboBox Selection

    lee704 Level 1

      Ist there a way to enable or disable a button based on the value selected in a ComboBox?  If so, what is the syntax?  Attached is what I have:

       

      <?xml version="1.0"?>

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

      <!-- Simple example to demonstrate the ComboBox control. -->

      <mx:Application

       

       

      xmlns:mx="http://www.adobe.com/2006/mxml"

      >

       

       

       

      <mx:Script>

      <![CDATA[

       

       

      import

      mx.collections.ArrayCollection;

       

      [

       

      Bindable

      ]

       

       

      public var cards:ArrayCollection = new

      ArrayCollection(

      [ {label:

       

      "Visa"

      , data:1},

      {label:

       

      "MasterCard"

      , data:2},

      {label:

       

      "American Express"

      , data:3} ]);

       

       

       

      private function closeHandler(event:Event):void

      {

      myLabel.text =

       

      "You selected: "

      + ComboBox(event.target).selectedItem.label;

      myData.text =

       

      "Data: "

      + ComboBox(event.target).selectedItem.data;

      }

      ]]>

       

       

      </mx:Script>

       

       

       

      <mx:Panel title="ComboBox Control Example" height="75%" width="75%" layout="horizontal">

       

       

       

      <mx:ComboBox dataProvider="{cards}" close="closeHandler(event);" id="cardsComboBox"/>

       

       

       

      <mx:VBox>

       

       

      <mx:Text text="Select a type of credit card."/>

       

       

      <mx:Label id="myLabel" text="You selected:"/>

       

       

      <mx:Label id="myData" text="Data:"/>

       

       

      <mx:Button label="Button" id="cardsButton" enabled="{cardsComboBox.selectedIndex.valueOf()}"/>

       

       

       

      </mx:VBox>

       

       

       

       

      </mx:Panel>

       

      </mx:Application>

       

       

      I need the "enabled" property of the "cardsButton" object to vary based on what the users selects in the "cardsComboBox" object.  If the user selects an item with a selectedIndox > 0, I want the button to be enabled.

       

      Any suggestion how to correct my code to accomplish this?

        • 1. Re: Enable button based on ComboBox Selection
          tcflex

          For the combo box, implement the changeHandler for change event.

           

          <mx:ComboBox dataProvider="{cards}" close="closeHandler(event);" id="cardsComboBox" change="changeHandler(event)"/>

           

          private function changeHAndler(evnt:ListEvent):void {

           

          if(event.currentTaraget.selectedIndex > 0)

            cardsButton.enabled = true;

          else

            cardsButton.enabled = false;

          }

           

          Thanks,

          tc.

          1 person found this helpful
          • 2. Re: Enable button based on ComboBox Selection
            lee704 Level 1

            Excellant!  Thank you.  The completed code is listed below:

             

                        private function changeHandler(event:Event):void
                        {
                            if (event.currentTarget.selectedIndex > 0)
                                cardsButton.enabled=true;
                            else
                                cardsButton.enabled=false;

             

                        }

             

                    <mx:ComboBox id="cardsComboBox"
                                 dataProvider="{cards}"
                                 close="closeHandler(event);"
                                 change="changeHandler(event)"/>

             

            As a follow-up, I need to add one more clause to my conditions.  I need to also check to see if CheckBox "ckCheckBox" is selected.  Only if this condition, plus the previous condition must be met before the cardsButton is enabled.  Can I just add a clause to the first condition like:

             

            if (event.currentTarget.selectedIndex > 0) and ckCheckBox.selected = TRUE

             

            if so, what would be the syntax?

             

            Thanks again!