1

I am trying to add a button to a group in a Flex 4 application from within a function like this;

public function addButton(myID:Number):void {
    var myButton:Button = new Button();
    myButton.id = ObjectUtil.toString(myID);
    myButton.label = "New Button "+myButton.id;
    myButton.click= textAlerter(myID);      
    myGroup3.addElement(myButton);
}

The label and id properties get added but not the click. The error message says click is an undefined property for spark.components:Button. But it is a property when add it to the group like this;

What am I doing wrong?

1 Answer 1

3

In MXML, click is not a property, it's a shortcut to an event listener.

public function addButton(myID:Number):void {
    var myButton:Button = new Button();
    myButton.id = myID.toString();
    myButton.label = "New Button "+myButton.id;
    myButton.addEventListener(MouseEvent.CLICK, textAlerter);
    myGroup3.addElement(myButton);
}

private function textAlerter(e:Event):void 
{
    var myID:String = (e.currentTarget as Button).id;
    //your code here
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.