0

I've got some buttons that have toggle set to true. I'm trying to re-set the button's state to unselected. But, I can't access the button directly like: button.selected=false.

I'm accessing the HBox's children which are the buttons. UIComonent doesn't have a selected property. So, how do I de-select the toggle in this bit of code below?

for (var j : int=0; j < theHBox.numChildren; j++){
   var child : DisplayObject = theHBox.getChildAt(j);
   var myButton:UIComponent = child as UIComponent;
   myButton.setStyle("borderColor", "blue");
   myButton.visible = true;
   } 
Laxmidi
  • 2,650
  • 12
  • 49
  • 81

1 Answers1

2

If possible, I would recommend casting the UIComponent to a button:

for (var j : int=0; j < theHBox.numChildren; j++){
   var child : DisplayObject = theHBox.getChildAt(j);
   if(child is Button){
    var myButton:Button = child as Button;
    myButton.setStyle("borderColor", "blue");
    myButton.visible = true;
   } else if(child is somethingElse){
     // do something else
   }
} 

You could also do something like this:

for (var j : int=0; j < theHBox.numChildren; j++){
   var child : DisplayObject = theHBox.getChildAt(j);
   var myButton:UIComponent = child as UIComponent;
   myButton.setStyle("borderColor", "blue");
   myButton.visible = true;
   myButton['toggle'] = false;
} 

Which will work if all children are buttons, but if the 'myButton' does not have a toggle property, it will throw a run time error.

JeffryHouser
  • 39,401
  • 4
  • 38
  • 59
  • Hi Flextras.com, Thank you very much for the help. I used option 1. I was able to get rid of the UIComponent. So, I changed the line to var myButton:Button = child as Button. The rest of your code I used unchanged. Thanks again. – Laxmidi Nov 03 '10 at 14:35
  • Good catch on my typo. That happens sometimes when I write code in the browser. I'll fix it in my answer. Glad to help! – JeffryHouser Nov 03 '10 at 20:21