Tuesday, May 22, 2012

Changing the state of a check box by setting selected property to true will not call the change event

Strange , but I was assuming that if I set the  "selected" property of a CkeckBox in flex it will call the change event :) but it wont. Below is an example


 
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
layout="absolute" creationComplete="init()">
<mx:Script>
    < ![CDATA[
        import mx.controls.Alert;
        function checkBoxClicked():void{
            Alert.show("Check box clicked");
        }
        
        function init():void{
            //cb.selected=true;
            cb.dispatchEvent(new MouseEvent(MouseEvent.CLICK));    
        }
    ]] >
</mx:Script>
    <mx:CheckBox x="93" y="43" id="cb" label="Checkbox"
 change="checkBoxClicked()"/>
    
</mx:Application> 
 




this didn't work , than I had to change the "init" function like below

        function init():void{
             cb.dispatchEvent(new MouseEvent(MouseEvent.CLICK));   
        }

Note now I am pragmatically calling the mouse click event and to my surprise it worked , that's not good or am I missing something?

So Let's start looking in to the API documentation which states

change

Event
Event Object Type: flash.events.Event
property Event.type = flash.events.Event.CHANGE
Language Version: ActionScript 3.0
Product Version: Flex 3
Runtime Versions: Flash Player 9, AIR 1.1
Dispatched when the selected property changes for a toggle Button control. A toggle Button control means that the toggle property is set to true. For the RadioButton controls, this event is dispatched when the selected property changes. For the CheckBox controls, this event is dispatched only when the user interacts with the control by using the mouse.

This is really strange but this is how things work in ActionScript.



2 comments:

  1. Forget Flex and start to use jQuery :)
    Asem

    ReplyDelete
  2. LOL , You have to support Legacy code rite ;).

    ReplyDelete