We help IT Professionals succeed at work.

How can i make a button stay in the down state until another is clicked?

How can i make a button stay in the down state until another is clicked?
Comment
Watch Question

Try using the ToggleButtonBar for navigation it comed directly with that functionality.

Or you use the toggle property of the Button component (have a look at this example):
http://livedocs.adobe.com/flex/3/html/help.html?content=controls_04.html
http://blog.flexexamples.com/2007/08/20/creating-toggle-and-emphasized-button-controls-in-flex/
Ups ... sorry ... just noticed you were talking about plain old flash and not flex. Try this:
http://www.actionscript.org/resources/articles/140/1/Making-a-toggle-button/Page1.html

Author

Commented:
That is great for sound Chris but we just want to overstate to stay in colour until another button is clicked. What code would you employ?
Thanks
well couldn't you just set the color in the onclick handler and have the handler of every button reset the color of the other buttons? I know this is not really extendable, but it should do the trick.

Author

Commented:
Hi Chris
Thanks for your response we may come back to you at a later date.
Vicky
1) put your buttons in an array.

2) in the onClick of each button call an isolated function e.g. resetButtons();

3) inside resetButtons, loop the array changing all buttons back to the original state.

4) Then change the one clicked to the down state.

Its more extend-able because you just need to add future buttons to your array.
This answer will probably be overkill, but just to add my two cents. For what you are describing the State Machine design pattern will work wonders. It will keep your code nicely encapsulated, object-oriented and will simplify future changes.

Of course, you have to be committed to using OO best practices, otherwise, like I said, this will be overkill.

But, if you want to expand your development horizons, here's some recommended reading:

http://sourcemaking.com/design_patterns/state
http://en.wikipedia.org/wiki/State_pattern

Here's an Adobe tutorial that makes the buttons play video, but if you ignore that part you can concentrate on their use of the State Design Pattern to do what you want to do (keep the button pressed until the next one is pressed)
http://www.adobe.com/devnet/flashmediaserver/articles/video_state_machine_as3.html

And here's the definitive resource for Flash, ActionScript 3 and Design patterns:
http://www.as3dp.com/category/design-patterns/state/

Best of luck! Don't shy away from taking your coding skills to the next level!

Tom

Author

Commented:
Thanks very much guys.