2 Replies Latest reply on Sep 22, 2008 7:37 AM by Newsgroup_User

    Identifying Image selected or not

    Sankar Kethineni Level 1
      Hi All,

      I have a scenario like this, 3 images are there and on click of each image am calling the same function and in that function i want to know on which image i have been clicked( In case of Button we have selected attribute, with the help of this attribute we can come to know which button has been clicked). Is there attribute like this for Image control. how do i do this?

      here is the sample code
      <?xml version="1.0" encoding="iso-8859-1"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute">
      <mx:Script>
      <![CDATA[

      private function check():void
      {
      // how do i check here
      }
      ]]>
      </mx:Script>
      <mx:VBox>
      <mx:Image id="image1" source="images/budget32x32.png" click="check()"/>
      <mx:Image id="image2" source="images/pay-bills.png" click="check()"/>
      <mx:Image id="image3" source="images/calendar_small.png" click="check()"/>
      </mx:VBox>
      </mx:Application>

      Thanks in Advance
        • 1. Re: Identifying Image selected or not
          Ventis85
          Your calling your method "check" on a click-event, so flex will dispatch a click-event.

          you can catch it like this:

          <mx:Image id="image1" source="images/budget32x32.png" click="check(event)"/>

          and you function:

          private function check(event:MouseEvent):void
          {
          // event.currentTarget is the actual clicked image here. It has all the properties of the image like id, source,...
          }

          If you give an id to your VBox which holds your images you can add an eventListener to that VBox instead of calling the click event on each image. Like so:

          <mx:VBox id="whateverNameYouWant">
          <mx:Image id="image1" source="images/budget32x32.png"/>
          <mx:Image id="image2" source="images/pay-bills.png"/>
          <mx:Image id="image3" source="images/calendar_small.png"/>
          </mx:VBox>

          in your script:

          whateverNameYouWant.addEventListener(MouseEvent.CLICK, check);

          Of course that means the event will also trigger if you click inside your VBox, but not on an image.

          • 2. Re: Identifying Image selected or not
            Level 7

            "Ventis85" <webforumsuser@macromedia.com> wrote in message
            news:gb82mq$q84$1@forums.macromedia.com...
            > Your calling your method "check" on a click-event, so flex will dispatch a
            > click-event.
            >
            > you can catch it like this:
            >
            > <mx:Image id="image1" source="images/budget32x32.png"
            > click="check(event)"/>
            >
            > and you function:
            >
            > private function check(event:MouseEvent):void
            > {
            > // event.currentTarget is the actual clicked image here. It has all the
            > properties of the image like id, source,...
            > }
            >
            > If you give an id to your VBox which holds your images you can add an
            > eventListener to that VBox instead of calling the click event on each
            > image.
            > Like so:
            >
            > <mx:VBox id="whateverNameYouWant">
            > <mx:Image id="image1" source="images/budget32x32.png"/>
            > <mx:Image id="image2" source="images/pay-bills.png"/>
            > <mx:Image id="image3" source="images/calendar_small.png"/>
            > </mx:VBox>
            >
            > in your script:
            >
            > whateverNameYouWant.addEventListener(MouseEvent.CLICK, check);
            >
            > Of course that means the event will also trigger if you click inside your
            > VBox, but not on an image.

            You'd also need to look at event.target instead of event.currentTarget (that
            will contain the vbox), which will probably be referring to a subcomponent
            of the Image control. It's probably simpler to add the eventlistener to all
            of the Images, then currentTarget will refer to the Image component as a
            whole.

            HTH;

            Amy