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()">
    < ![CDATA[
        import mx.controls.Alert;
        function checkBoxClicked():void{
            Alert.show("Check box clicked");
        function init():void{
            cb.dispatchEvent(new MouseEvent(MouseEvent.CLICK));    
    ]] >
    <mx:CheckBox x="93" y="43" id="cb" label="Checkbox"

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


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.


  1. Forget Flex and start to use jQuery :)

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