How to set property of every button?

I’m making a “soundboard” app, where there’s 12 buttons and each triggers a different sound. When a button is pressed I’d like to make that button turn red, and set the color of the other buttons to blue.

I know I could do that in a “when button1.click” routine, but that’s a lot of code.

Can anyone think of a more elegant way to do that?

Is there some way to set the background color of all the buttons, and then I could set the background color of the last button pressed afterwards?

Thanks for any help.

You can use the any button function described here: "Any Component" Blocks - ✕ Docs

Thanks for that. That works for making the newly clicked button red, but how to turn every other button blue?

In other words:

  • I have a grid of blue buttons.

  • When I press a button I want that button to turn red.

  • When I press another button I want that new button to be red and all other buttons to be blue.

Any advice for turning all other buttons blue?

And is there any way to apply some logic to that action, so I can execute that action when only some buttons are clicked? (In other words, not every button should turn red when clicked)

Hey @wrybread
So here how I did this.

  1. Create a function that takes in a variable

  2. Using the “When Any Button” Dialog is clicked call that function and pass it the component

  3. In the function first set all the buttons backgrounds to blue

  4. Finally set the color of the the input variable to red

This worked for me in my app :slight_smile: The image below shows the exact blocks

If this answered your question please don’t forget to mark my answer as the solution :smiley:

As a note when you create a function with an input. It shows up in the “variables” screen

2 Likes

Thanks, I’m almost there. The only thing I can’t figure out now is how to have some buttons be an exception. In other words I have a few buttons that shouldn’t change color when pressed. Can anyone think of some way to ignore the presses of a specific button?

To avoid specific buttons, would would have to name them specifically as exception. Or makes them have a very specific condition that can be identified.
For instance, if the buttons you do not want to have changed all had their background color set to orange (for instance) then if your just be a matter of making a test “if button j get background_color not_equal_to ‘orange’ do” (set to blue).

The idea is that you find a way of making them stand out so that a test can be implemented that just states “avoid this one”

Thank you! Works great.

screen2