1 Reply Latest reply on Jan 8, 2010 10:19 AM by RK...

    how to include a component based on a condition using actionscript

    sreedharkv

      I have an mxml application and  have developed two custom components.  Based on a condition (combo box selection), a custom developed component needs to be included. How can this be achieved using actionscript ?

        • 1. Re: how to include a component based on a condition using actionscript
          RK... Level 3

          Hope this code will help you,

           

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application 
               xmlns:mx="http://www.adobe.com/2006/mxml" 
               layout="absolute"
               creationComplete="init()">
               <mx:Script>
                    <![CDATA[
                         import com.MyComp2;
                         import com.MyComp1;
                         
                         private var _myComp1: MyComp1 = null;
                         private var _myComp2: MyComp2 = null;
                         
                         private function init(): void {
                              _myComp1 = new MyComp1();
                              _myComp1.x = 50;
                              _myComp1.y = 50;
                              _myComp2 = new MyComp2();
                              _myComp2.x = 50;
                              _myComp2.y = 50;
                         }
                         
                         private function addComponent(event: Event): void {
                              switch (cbComponent.selectedItem) {
                                   case "Comp 1":
                                        if (this.contains(_myComp2)) {
                                             this.removeChild(_myComp2)
                                        }
                                        this.addChild(_myComp1)
                                   break;
                                   case "Comp 2":
                                        if (this.contains(_myComp1)) {
                                             this.removeChild(_myComp1)
                                        }
                                        this.addChild(_myComp2)
                                   break;
                              }
                         }
                    ]]>
               </mx:Script>
               
               <mx:ComboBox id="cbComponent" change="addComponent(event)">
                    <mx:Array>
                         <mx:String>Comp 1</mx:String>
                         <mx:String>Comp 2</mx:String>
                    </mx:Array>
               </mx:ComboBox>
          </mx:Application>