4 Replies Latest reply on Jan 12, 2011 8:36 AM by superkatakam

    Identifying the parent of the component where event occured.

    superkatakam Level 1

      I need to add a HBox in another VBox everytime a button is clicked.. The HBox is supposed to contain 3 components. 2 comboboxes and a combobox,textinput or datefield depending on the selected option in the first combobox. If I am adding the HBox dynamically upon clicking a button, how can I identify the combobox that has been clicked and change the corresponding 3rd component of the HBox.

      In other words, how can I find out the component in which HBox has been clicked and deal with it accordingly?

      Your help is greatly appreciated.

      Thank you

      Nikhil

        • 2. Re: Identifying the parent of the component where event occured.
          superkatakam Level 1

          event.target would only give me the combobox i've clicked on. I need to know the HBox in which the combobox is located.

          • 3. Re: Identifying the parent of the component where event occured.
            Gregory Lafrance Level 6

            This code should get you started, and answers your question:

             

            <?xml version="1.0"?>
            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
              <mx:Script>
                <![CDATA[
                  import mx.controls.DateField;
                  import mx.controls.TextInput;
                  import mx.collections.ArrayCollection;
                  import mx.controls.ComboBox;
                  import mx.containers.HBox;
                  import mx.events.ListEvent;
                  
                  private function addComponents():void{
                    var hb:HBox = new HBox();
                    var cmbx1:ComboBox = new ComboBox();
                    var cmbx2:ComboBox = new ComboBox();
                    cmbx1.prompt = "Select component";
                    cmbx1.dataProvider = new ArrayCollection(["ComboBox", "TextInput", "DateField"]);
                    cmbx1.addEventListener(ListEvent.CHANGE, addNew);
                    cmbx2.dataProvider = new ArrayCollection(["one", "two", "three"]);
                    hb.addChild(cmbx1);
                    hb.addChild(cmbx2);
                    addChild(hb);
                  }
                  
                  private function addNew(evt:ListEvent):void{
                    var parentComp:HBox = ComboBox(evt.currentTarget).parent as HBox;
                    switch(evt.currentTarget.selectedLabel){
                      case "ComboBox":
                        var newCmbx:ComboBox = new ComboBox();
                        newCmbx.dataProvider = new ArrayCollection(["new1", "new2", "new3"]);
                        parentComp.addChildAt(newCmbx, 2);
                        break;
                      case "TextInput":
                        var newTi:TextInput = new TextInput();
                        parentComp.addChildAt(newTi, 2);
                        break;
                      case "DateField":
                        var newDf:DateField = new DateField();
                        parentComp.addChildAt(newDf, 2);
                        break;
                    }
                  }
                ]]>
              </mx:Script>
              <mx:Button click="addComponents()" label="Add Components"/>
              <mx:VBox id="vb" width="100%" height="100%"/>
            </mx:Application>
            


            If this post answers your question or helps, please mark it as such. Thanks!

            http://www.stardustsystems.com
            Adobe Flex Development and Support Services

            • 4. Re: Identifying the parent of the component where event occured.
              superkatakam Level 1

              Thank you Gregory. You're a life saver!