Looping over RadioButton controls in a RadioButtonGroup in Flex

by Peter deHaan on June 20, 2008

in RadioButton, RadioButtonGroup

The following example shows how you can loop over the RadioButton controls in a Flex RadioButtonGroup by using the numRadioButtons property and getRadioButtonAt() method.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/06/20/looping-over-radiobutton-controls-in-a-radiobuttongroup-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            import mx.controls.Alert;

            private function showAlert():void {
                var arr:Array = [];
                var rb:RadioButton;
                var idx:int;
                var len:int = radioGroup.numRadioButtons;

                for (idx=0; idx<len; idx++) {
                    rb = radioGroup.getRadioButtonAt(idx);
                    arr.push("index:" + idx + ", label:" + rb.label);
                }
                Alert.show(arr.join("\\n"), "getRadioButtonAt():");
            }
        ]]>
    </mx:Script>

    <mx:ApplicationControlBar dock="true">
        <mx:Button label="Click me" click="showAlert();" />
    </mx:ApplicationControlBar>

    <mx:RadioButtonGroup id="radioGroup" />

    <mx:VBox>
        <mx:RadioButton id="radioButton1" label="One" />
        <mx:RadioButton id="radioButton2" label="Two" />
        <mx:RadioButton id="radioButton3" label="Three" />
        <mx:RadioButton id="radioButton4" label="Four" />
        <mx:RadioButton id="radioButton5" label="Five" />
    </mx:VBox>

</mx:Application>

View source is enabled in the following example.

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can 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

Previous post:

Next post: