开发者

add a Button with function in Flex 4; click?

开发者 https://www.devze.com 2023-02-19 08:47 出处:网络
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 {

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?


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
}
0

精彩评论

暂无评论...
验证码 换一张
取 消