FLEX Button event handling

316 Views Asked by At

I have a scenario where i have 5 buttons which call the same method when clicked. These buttons are clicked in various conditions, but now i want to know how we determine that which particular button has been clicked, from the called method.

For example, i have been calling chocolate() method when i click the buttons, eclairs, dailrymilk, cadbury, snickers and kitkat. Now i will click anyof these buttons from the UI and i want to know which one is clicked. this event has to be handled in the chocolate() method only.

Please suggest me how can i implement this. I am using Adobe Flex 3

1

There are 1 best solutions below

0
Philarmon On

If you are not using the addEventListeners but are setting the click property in your buttons you could do something like that:

<s:Button id="snickers"
          click="{chocolate('snickers')}"
          label="snickers"/>

<s:Button id="kitkat"
          click="{chocolate('kitkat')}"
          label="kitkat"/>

private function chocolate(type:String):void
{
    trace("button", type, "was clicked");

    if(type == "snickers")
    {
        // do stuff
    }
    else if(type == "kitkat")
    {
        // do something else
    }
}

if you are working with event listeners you could determine the buttons from their ids, for example:

<s:Button id="snickers"
          label="snickers"/>

<s:Button id="kitkat"
          label="kitkat"/>

// add your event listeners somewhere like in onCreationComplete
snickers.addEventListener(MouseEvent.CLICK, chocolate);
kitkat.addEventListener(MouseEvent.CLICK, chocolate);

private function chocolate(e:MouseEvent):void
{
    // e.target is the component that has dispatched the event (a button in this case)
    var type:String = e.target.id;

    trace("button", type, "was clicked");

    if(type == "snickers")
    {
        // do stuff
    }
    else if(type == "kitkat")
    {
        // do something else
    }
}