tags:

views:

25

answers:

2

Hi,

How do I, in effect, "reset" a component in order to have it look the the way it did when it first loaded. For example, I've got 3 buttons in an HBox. They start as red, visible, and have a label. I then programmatically make different changes to them-- change the color of some of them, change the visibility of some of them, etc.

I then need to "reload" this HBox, have it revert back to the way it looked at the start. Is there an easy way to do this? (I have a lot of components that need to be changed).

<mx:HBox>
    <mx:Button id="button1"
        label="button1" 
        fillColors="[red, red]" 
        toggle="true" click="myClickHandler"/>

    <mx:Button id="button2" 
        label="button1" 
        fillColors="[red, red]" 
        toggle="true" click="myClickHandler"/>

    <mx:Button id="button3" 
        label="button1" 
        fillColors="[red, red]"  
        toggle="true" click="myClickHandler"/>
</mx:HBox>

If you have a suggestion, please let me know. Thank you.

-Laxmidi

+1  A: 

You are already problematically changing this code at runtime. Just write a method to change it back to it's default state. That is probably what I'd do.

Alternately, if this is an encapsulated component, you could always remove it with removeChild, create another instance, and put that new one in the same place.


Per comments, here is some psuedo code for looping over children of a component and changing properties:

for (var i : int =0; i<hBox.numChildren; i++){
  var child : UIComponent = hBox.getChildAt(i);
  child.setStyle('style','defaultValue');
  child.property = 'default value'
}
www.Flextras.com
Hi Flextras, I guess that I'll have to write a method to revert it back. Is there a way to refer to all of the buttons in the HBox? Pseudo-code: Something like HBox1.button.setStyle("fillcolors", "[red, red]"); or HBox1.child.button.setStyle("fillcolors", "[red, red]");. Or is a loop the only way to do it? I could set i to the button's id number.
Laxmidi
hi Flextras, same thing u said, i read it later,and i posted ma answer, n e ways, hav a gr8 time both of u,
Ankur Sharma
@Laxmidi with your current code, you just could reference the buttons by ID. Although you'll need to add an ID to the HBox. So, hBox.button1.label = "button1". If you wanted to be more flexible, loop over the children of the component. I'll add some psuedo code for that to my answer.
www.Flextras.com
What Flextras said, so you'll have a loop within a loop. Loop through the components and while inside the loop, check the children of the component and loop through them as well. If you just want to change the buttons style, check the object to make sure it's of the correct type.
Organiccat
Depending on the component structure, it could be a recursive loop.
www.Flextras.com
Hi Flextras.com, Thank you very much for the code. I only had to convert the displayObject into a UIComponent. Last night, I was working on a loop, but I was targetting Button instead of DisplayObject. Why do I have to target DisplayObject instead of Button? For anyone who is interested, the woring code looks like this: for (var i : int =0; i< hBox.numChildren; i++){  var child : DisplayObject = hBox.getChildAt(i);var child2:UIComponent = child as UIComponent;child2.setStyle('themeColor','haloGreen');} Thank you for the excellent help.
Laxmidi
My code gets the child as a UIComponent, not a DisplayObject and not as a Button. I'm not sure why your code wouldn't work.
www.Flextras.com
A: 
<mx:Application>
    <mx:Script>
        private function onColorChange():void
                {
                    can.removeAllChildren();

                    var loader:Loader = new Loader();
                    loader.load(new URLRequest('assets/images/logo/1.png'));
                    loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete);

                    /* image= new Image();
                    image.source = "assets/images/logo/1.jpg";
                    image.setStyle('horizontalCenter','0');
                    image.setStyle('verticalCenter','0'); */

                    //can.addChild(image);

                    txt= new Text();
                    txt.text = "Ankur sharma";
                    txt.styleName = "font";
                    txt.setStyle('fontFamily','Anime Ace');
                    txt.rotation = -10;

                    can.addChild(txt);

                    can.mask = txt;
                    //applyFilter(CC.uint2rgb(cp.selectedColor));
                }

                private function onComplete(event:Event):void
                {
                    rect = new Rectangle();
                    rect = txt.getBounds(can);  

                    can.graphics.clear();   
                    can.graphics.beginBitmapFill(event.currentTarget.content.bitmapData);
                    can.graphics.drawRect(rect.x,rect.y,rect.width,rect.height);
                    can.graphics.endFill();

                }
    </mx:Script>

    <mx:ColorPicker id="cp" change="onColorChange()"/>
        <mx:Canvas id="can" height="100%" horizontalCenter="0" verticalCenter="0" borderStyle="solid" borderColor="#CCCCCC" borderThickness="5">
            <mx:Image source="assets/images/logo/1.png" horizontalCenter="0" verticalCenter="0"/>
            <mx:Text text="Ankur Sharma" styleName="font" rotation="-10"/>
        </mx:Canvas>
        <mx:Style source="style.css"/>
    </mx:Application>

in this example, wht i m doin is i m removing all children in ma canvas(id=can) amd then then makeing n e changes, to exizting components and then adding thm back to the canvas,

this program is of masking n e ways, my canvas has two children, and i m putting ma text as a mask over the canvas, and i am filling the canva with bitmap image, thats it

i hop it hepls

Ankur Sharma