views:

29

answers:

2

Hi,

I've got a few HBoxes with buttons in them. I programmatically make certain buttons invisible. At a certain point, all of the buttons should be invisible. How can I tell when all of the buttons are invisible? What's the easiest way of doing so?

Each button's visibility is determined independently of the other buttons.

<mx:HBox>
    <mx:Button id="button1" 
    click="clickHandler(event);" 
    toggle="true"
    visible=true/>

    <mx:Button id="button2" 
    click="clickHandler(event);" 
    toggle="true"
    visible=false/>

    <mx:Button id="button3" 
    click="clickHandler(event);" 
    toggle="true"
    visible=true/>
</mx:HBox>

<mx:HBox>
    <mx:Button id="button4" 
    click="clickHandler(event);" 
    toggle="true"
    visible=false/>

    <mx:Button id="button5" 
    click="clickHandler(event);" 
    toggle="true"
    visible=true/>

    <mx:Button id="button6" 
    click="clickHandler(event);" 
    toggle="true"
    visible=false/>
</mx:HBox>

Thank you.

-Laxmidi

+2  A: 

Easiest way is not necessarily the best way but something like this should work...

public function areAllButtonsInvisible() : Boolean {
    for ( var i : int = 1; i < 7; i++ ) {
        if ( ( this["button"+i] as UIComponent ).visible {
            return false;
        }
    }
    return true;
}
Gregor Kiddie
+1, I was thinking the same thing :)
Wade Mueller
You can store them in a private array (initialized in creationComplete) to make it a bit more elegant.
Amarghosh
Hi Gregor Kiddie, Thank you very much for your function. In the end, I found an easier solution. I've got a function that handles whether the button is made invisible or not. I created a variable set to the total number of buttons and then decremented it each time a button was made invisible. Again, thank you very much for your kind help.
Laxmidi
+1  A: 

Answer from Gregor above works for all buttons in the component, but if you just want to check for the buttons inside a certain HBox you could use the "some" function on the children Array of the HBox component like this:

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">
<mx:Script>
    <![CDATA[
        import mx.core.UIComponent;

        private function clickHandler(event:MouseEvent):void{
            (event.target as UIComponent).visible = false;
            buttonsVis.text = box.getChildren().some(isVisible).toString();
        }

        private function isVisible(item:*, index:int, array:Array):Boolean{
            return (item as UIComponent).visible;
        }

    ]]>
</mx:Script>
<mx:HBox id="box">
    <mx:Button id="button1" 
               click="clickHandler(event);" 
               toggle="true"
               visible="true"/>

    <mx:Button id="button2" 
               click="clickHandler(event);" 
               toggle="true"
               visible="false"/>

    <mx:Button id="button3" 
               click="clickHandler(event);" 
               toggle="true"
               visible="true"/>
</mx:HBox>
<mx:Label text="Buttons are Visible: "/><mx:Label id="buttonsVis" text="true"/>

rosswil
Hi rosswil, Thank you for your solution. In my case, I need to test against all of the HBoxes. In any event, thank you for the excellent suggestion.
Laxmidi