3 Replies Latest reply on Aug 10, 2006 8:13 PM by doug777

    Radio Button Groups

    doug777 Level 1
      I have created components in loops on many occasions now, but the method doesn't seem to work with Radio Button Groups.

      Even though on each iteration I create a new group, subsequently all buttons belong to a single group as you can see:

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" xmlns="*" creationComplete = "initApp()">
      <mx:Script>
      <![CDATA[
      import mx.containers.HBox;
      import mx.controls.RadioButton;
      import mx.controls.RadioButtonGroup;

      public function initApp():void{
      var hb:HBox;
      var rbg:RadioButtonGroup;
      var rb:RadioButton;
      var j:uint;
      var label:String;
      var rbarray:Array;

      for(var i:uint=0; i<4; i++){
      hb = new HBox();
      page.addChild(hb);
      rbg = new RadioButtonGroup();
      for(j=0; j<2; j++){
      rb = new RadioButton();
      rb.groupName = "rbg";
      if(Boolean(j)){
      label = "No";
      }else{
      label = "Yes";
      }
      rb.label = label;
      rb.labelPlacement = "left";
      rb.value = label;
      //rbarray.push(rb);
      hb.addChild(rb);
      }
      rb = new RadioButton();
      rb.groupName = "rbg";
      rb.label = "No preference";
      rb.labelPlacement = "left";
      rb.selected = true;
      hb.addChild(rb);
      }
      }
      ]]>
      </mx:Script>

      <mx:Panel id="page" width="300" height="150" />

      </mx:Application>

      Also I would like to keep the Radio Button instance names in an array as I have done before, but if you uncomment the commented out line on this occasion you get an error.

      Is there any way to build Radio Button Groups using a loop method?

      And why can't I store the names of the buttons?

      Doug
        • 1. Re: Radio Button Groups
          doug777 Level 1
          I should have been making an array of RadioButtonGroups not RadioButtons as follows:

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" xmlns="*" creationComplete = "initApp()">
          <mx:Script>
          <![CDATA[
          import mx.containers.HBox;
          import mx.controls.RadioButton;
          import mx.controls.RadioButtonGroup;

          public function initApp():void{
          var hb:HBox;
          var rbg:RadioButtonGroup;
          var rb:RadioButton;
          var j:uint;
          var label:String;
          var rbgarray:Array = new Array();
          var n:uint = 0;

          for(var i:uint=0; i<4; i++){
          hb = new HBox();
          page.addChild(hb);
          rbg = new RadioButtonGroup();
          rbgarray.push(rbg);
          for(j=0; j<2; j++){
          rb = new RadioButton();
          rb.groupName = rbgarray[n];
          if(Boolean(j)){
          label = "No";
          }else{
          label = "Yes";
          }
          rb.label = label;
          rb.labelPlacement = "left";
          rb.value = label;
          hb.addChild(rb);
          }
          rb = new RadioButton();
          rb.groupName = rbgarray[n];
          rb.label = "No preference";
          rb.labelPlacement = "left";
          rb.selected = true;
          hb.addChild(rb);
          n++;
          }
          }
          ]]>
          </mx:Script>

          <mx:Panel id="page" width="300" height="150" />

          </mx:Application>

          But the snag is that RadioButtonGroups don't expose their instance name at all and therefore there is no way to refer to them, is there??

          Doug
          • 2. Re: Radio Button Groups
            inlineblue Level 1
            Setting RadioButton.groupName works only if you give it the name of a group that's a child of the enclosing MXML document (ie. usually that means it's declared in the MXML). If you create the group dynamically, you'll have to assign the group directly. eg:

            rb.group = rbg;

            And you're getting an error with the array because you didn't instantiate rbarray (it's still null).
            • 3. Re: Radio Button Groups
              doug777 Level 1
              Yes changing groupName to group in the second set of code I posted does indeed work correctly.

              Can nothing be done about improving the documentation for AS users? It's almost impossible to work out what the solution is to these sort of problems when the documentation misses out important information.

              However the help I have had from these forums is absolutely invaluable.

              As always, thanks so much!

              Doug