0

i am trying to make a some groups within a predefined group at run time. i can make groups, the code is working fine. Now i have added two buttons in each group which is created in run time. now the problem is one of the button is x . the property of button should be when i click on it it should delete the very specific group in which it is built. please check out the code and tell me what additions should i make.

            public var n:Number = 0;

        protected function remover (event:MouseEvent):void
        {
        }

        protected function settings(g:Group):void
        {
            ((DisplayObject)(g)).width = "200";
            ((DisplayObject)(g)).height = "140";
            ((DisplayObject)(g)).x = "200" ;
            ((DisplayObject)(g)).y = "200";
            var s:String;

            s = "NewGroup"+ "X"+ n.toString;
            var mybutton1:Button = new Button();
            mybutton1.id = s;
            mybutton1.label ="X";
            mybutton1.addEventListener(MouseEvent.CLICK, remover);
            g.addElement(mybutton1);
    //      setting1(mybutton1);
            n++;
        }

        public function addGroup(event:MouseEvent): void
        {
            var s:String;
            n = n+1;
            s = "NewGroup"+ "_"+ n.toString;
            var myGroup:Group = new Group();
            myGroup.id = s;
            main.addElement(myGroup);
        //  setElementIndex(myGroup, n);
            settings(myGroup); 
        }

    ]]>
</fx:Script>

<s:Button x="422" id="wow"   y="139" label="Add Group"  click="addGroup(event)"/>

<s:HGroup id="main" x="6" y="168" width="750" height="490">
</s:HGroup>
Muhammad Umar
  • 11,391
  • 21
  • 91
  • 193
  • Why are you creating these groups at runtime? What are you trying to accomplish? What's the problem you want to solve? – J_A_X Sep 26 '11 at 04:15
  • i am trying to add a group container and a "X" button associated with it. i am adding these two things whenever i click on "add group" button. once i have made some groups lets say 2 whenever i click on the corresponding "x" button it should delete itself and the group associated with it. the problem is in the remover function i dont know how to get the index of that x button on which i clicked – Muhammad Umar Sep 26 '11 at 06:17

2 Answers2

1

You need to look into using DataGroup to create ItemRenderers (your groups) which has a button within it. Everything is based on a data collection (like ArrayCollection) that you manage. It makes everything easier because if you bind the ArrayCollection to the dataProvider property of the DataGroup and remove an item from the collection, your DataGroup will automatically update and remove the display item for you.

J_A_X
  • 12,857
  • 1
  • 25
  • 31
1

This should work:

protected function remover(event:MouseEvent):void
{
  main.removeElement(Group(Button(event.currentTarget).parent));
}
grass
  • 176
  • 1
  • 12