RefMa77
RefMa77

Reputation: 293

Add Event-Listener for the whole Symbol

I´ve put a Label and a ComboBox together in a Symbol, like in the code below.

public class LabelCombo extends MovieClip {     


        //myLabel is an instance of fl.controls.Label
        //myCombo is an instance of fl.controls.ComboBox
        public function LabelCombo() {              
        }


        public function setLabelText(s:String):void{
            myLabel.text = s;
        }

        public function getLabelText():String{
            return myLabel.text;
        }



        public function removeAllItems():void{
            if(myCombo.dataProvider.length > 0)
                myCombo.removeAll();
        }


        public function setSingleItem(o:Object):void{
            removeAllItems();
            myCombo.addItem(o);
        }


        public function setList(itemList:Array):void{
            for(var i:int = 0; i < itemList.length; i++){
                if(i==0)
                    setSingleItem(itemList[i]);
                else
                    addSingleItem(itemList[i]);
            }
        }


        public function addSingleItem(o:Object):void{
            for(var i:int = 0; i < myCombo.dataProvider.length; i++){
                    if(o == myCombo.getItemAt(i))
                        return;
                }
            myCombo.addItem(o);
        }


        public function addList(itemList:Array):void{
            for(var i:int = 0; i < itemList.length; i++){
                addSingleItem(itemList[i]);
            }
        }

        public function getSelectedItem():Object{
            return myCombo.selectedItem;
        }

        public function getItemByLabel(s:String):Object{
            return Object(myCombo.getChildByName(s));
        }

        public function selectItemAt(index:int):void{
            myCombo.selectedIndex = index;
        }

        public function getselectedIndex():int{
            return myCombo.selectedIndex;
        }

    }

Now I want to addEventListener(Event.CHANGE, changeHandler) on the whole Symbol so I can do something like that

trace(LabelCombo(event.currentTarget).getLabelText())

trace(LabelCombo(event.currentTarget).getSelectedItem().data)

I need information from both single controls. It`d be nice if someone can show me how to do this.

Thanks in advance

EDIT: I´ve recognized that there are some misunderstandings: I need this for a communication via XMLSocket. The server has to know two things: 1) what is the name of the ComboBox (in this case I get the name with myLabel) 2) the new selected Index Hope now everything is much clearer

Upvotes: 0

Views: 156

Answers (3)

VC.One
VC.One

Reputation: 15881

All I meant was instead of this:

trace(LabelCombo(event.currentTarget).getLabelText())

try like this: (if possible)

LabelCombo.setLabelText("Just_A_Test");
var str_Check : String = LabelCombo.getLabelText();
trace ("returned string is : " + str_Check);


It should say returned string is : Just_A_Test...


If LabelCombo is a child of myCombo then the correct reference path is: myCombo.LabelCombo.getLabelText(); etc etc.

Upvotes: 0

harilalkm
harilalkm

Reputation: 456

I really don't understand you question but you could try this

public class LabelCombo extends MovieClip {     
        //myLabel is an instance of fl.controls.Label
        //myCombo is an instance of fl.controls.ComboBox
        public function LabelCombo() { 
              myLabel.addEventlistener(Event.CHANGE,onChange);
              myCombo.addEventlistener(Event.CHANGE,onChange);

        }
        private function onChange(e:Event):void {
             dispatchEvent(new Event(Event.CHANGE));
        } 
        }

add change event listeners to the label and combo. Using dispatchEvent you could dispatch custom event . You could use like this

var labeCombo:LabelCombo = new LabelCombo();
addChild(labeCombo);
labeCombo.addEventlistener(Event.CHANGE,onChangeMv);
function onChangeMv(e:Event){
    trace(labeCombo.getLabelText());
    trace(labeCombo.getSelectedItem().data);
}

You can use custome event class to send data along with the event dispatcher.

Upvotes: 1

Vesper
Vesper

Reputation: 18747

You just set the event listener to myCombo but the listener should be located in the code of LabelCombo, this way you won't need tricks with event.currentTarget and just use this context to work with children, as the listener will only be triggered by changes in this object's myCombo child.

myCombo.addEventListener(Event.CHANGE,onChange,false,0,true);
function onChange(e:Event):void {
    trace(getSelectedItem().data);
}

Upvotes: 0

Related Questions