Search code examples
arraysactionscript-3checkboxselected

as3 checkbox selected in array


I have 20 checkbox in array, not label all checkboxes. And there are 3 dynamic text. All checkbox enabled true. I want, if any checkbox selected, first textfield selected checkbox name write. second and third selected likewise second and third textfiled write. end of 17 checkbox to get a enabled false

   import fl.controls.CheckBox;
   var arr:Array = [cb1,cb2,cb3,cb4,cb5,cb6,cb7,cb8,cb9,cb10,......]; //20 Checkbox

   // how do I find i = selected checkbox in array    

   i.addEventListener(MouseEvent.CLICK,myselect);
   function myselect(e:MouseEvent):void{
    trace(i.name);
       if (i.selected==true){
       yaz.text="Select"+" "+i.name;
}else {yaz.text="";}
}

Solution

  • As i think you need (document class):

    package  {
    
        import fl.controls.CheckBox;
        import flash.display.Sprite;
        import flash.events.MouseEvent;
        import fl.controls.Label;
    
        public class Test extends Sprite {
    
            public var cbBox:Sprite;
            public var tfBox:Sprite;
    
            public function Test() {
                init();
            }
    
            public function init()
            {
                cbBox = new Sprite();
                cbBox.x = 10;
                addChild(cbBox);
    
                tfBox = new Sprite();
                tfBox.x = 200;
                addChild(tfBox);
    
                for (var i:uint=0; i<20; i++)
                {
                    var cb:CheckBox = new CheckBox();
                    cb.addEventListener(MouseEvent.CLICK, onClickHandler);
                    cb.label = (i + 1).toString();
                    cb.y = i* 20;
                    cbBox.addChild(cb);
                }
            }
    
            public function onClickHandler(event:MouseEvent):void
            {
                if (tfBox.numChildren < 3)
                {
                    var label:Label = new Label();
                    label.width = 200;
                    label.height = 20;
                    label.y = tfBox.numChildren * 20;
                    label.text = CheckBox(event.target).label;
                    tfBox.addChild(label);
                }
    
                if (tfBox.numChildren == 3)
                {
                    for (var i:uint = 0; i< cbBox.numChildren; i++)
                    {
                        var ch:CheckBox = cbBox.getChildAt(i) as CheckBox;
                        if (!ch.selected) ch.enabled = false;
                    }
                }
            }
        }
    }