The following example shows how you can easily toggle a group of Spark CheckBox controls in Flex 4 by putting them in a nested container and using the getElementAt() method to get the individual CheckBox controls and set their selected property.

The following example(s) require Flash Player 10 and the Adobe Flex 4 SDK. To download the Adobe Flash Builder 4 trial, see http://www.adobe.com/products/flex/. To download the latest nightly build of the Flex 4 SDK, see http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4.
For more information on getting started with Flex 4 and Flash Builder 4, see the official Adobe Flex Team blog.

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/04/14/toggling-a-group-of-spark-checkbox-controls-in-flex-4/ -->
<s:Application name="Spark_Group_getElementAt_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/mx">
 
    <fx:Script>
        <![CDATA[
            protected function tgBtn_changeHandler(evt:Event):void {
                var idx:uint;
                const len:uint = chGr1.numElements;
                for (idx = 0; idx < len; idx++) {
                    var elmt:CheckBox = chGr1.getElementAt(idx) as CheckBox;
                    elmt.selected = tgBtn.selected;
                }
            }
        ]]>
    </fx:Script>
 
    <s:Panel id="pnl"
            title="Spark Panel title"
            horizontalCenter="0" verticalCenter="0">
        <s:controlBarContent>
            <s:ToggleButton id="tgBtn"
                    label="Toggle CheckBoxes"
                    change="tgBtn_changeHandler(event);" />
        </s:controlBarContent>
        <s:VGroup id="chGr1"
                gap="0"
                left="10" right="10" top="10" bottom="10">
            <s:CheckBox id="ch1" label="CheckBox One" />
            <s:CheckBox id="ch2" label="CheckBox Two" />
            <s:CheckBox id="ch3" label="CheckBox Three" />
            <s:CheckBox id="ch4" label="CheckBox Four" />
            <s:CheckBox id="ch5" label="CheckBox Five" />
            <s:CheckBox id="ch6" label="CheckBox Six" />
            <s:CheckBox id="ch7" label="CheckBox Seven" />
            <s:CheckBox id="ch8" label="CheckBox Eight" />
            <s:CheckBox id="ch9" label="CheckBox Nine" />
        </s:VGroup>
    </s:Panel>
 
</s:Application>

View source is enabled in the following example.

This entry is based on a beta version of the Flex 4 SDK and therefore is very likely to change as development of the Flex SDK continues. The API can (and will) change causing examples to possibly not compile in newer versions of the Flex 4 SDK.

 
Tagged with:
 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

One Response to Toggling a group of Spark CheckBox controls in Flex 4

  1. dragovian says:

    Nice one, but wouldn’t it be more performing to use a for each where the value you get is a checkbox :) ?

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree