cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
thesocrat
Frequent Visitor

Click a button to change a rectangle's color

It sounds so simple, but I can't figure it out.  I want to click on the Button1 button to change the color of the Rectangle1 icon from Blue to Green.  This should involve changing the OnSelect property of Button1 to Rectangle1.Fill = Green, but that doesn't do anything.  What is the correct syntax for this?  I can manually change Rectangle1's fill color from Blue to Green, but I can't do it with a button.

1 ACCEPTED SOLUTION

Accepted Solutions
Nogueira1306
Super User
Super User

Hey!

 

On app OnStart write this:

Set(varColor; RGBA(255;255;255;1);;

 

On rectangle fill property write:

varColor

 

On that button write:

 

Set(varColor; RGBA(11;156;49;1)

 

If you need additional help please tag me in your reply and please like my reply.
If my reply provided you with a solution, pleased mark it as a solution ✔️!

Best regards,

Gonçalo Nogueira

Check my LinkedIn!

Buy me a coffee!

Check my User Group (pt-PT)!

Last Post on Community

My website!

 

View solution in original post

3 REPLIES 3
Nogueira1306
Super User
Super User

Hey!

 

On app OnStart write this:

Set(varColor; RGBA(255;255;255;1);;

 

On rectangle fill property write:

varColor

 

On that button write:

 

Set(varColor; RGBA(11;156;49;1)

 

If you need additional help please tag me in your reply and please like my reply.
If my reply provided you with a solution, pleased mark it as a solution ✔️!

Best regards,

Gonçalo Nogueira

Check my LinkedIn!

Buy me a coffee!

Check my User Group (pt-PT)!

Last Post on Community

My website!

 

That worked, thanks.  I'm surprised that there isn't a simpler way to do it, like just writing Rectangle1.Fill = Blue.  It seems as if PowerApps will let controls derive their properties from other controls, but won't allow controls to impose properties on something else.

 

What's also confusing is why I can't seem to return the value of Rectangle1.Fill.  If I set that as the Text property for a label, it just shows a formula error.  It works if I set it to Rectangle1.Width, or Rectangle1.Font, but not Rectangle1.Fill.

Its best to think of those properties in terms of MSExcel - you don't set another cell by writing 'set cellA2 to x', you go to that cell and say 'if cellA1 is 4, then I am Blue'

There are declarative functions (define a value) that use inputs from other things to set themself and there are imperative functions that perform an action when you use them. Jukka Niiranen actually did a great tweet covering these recently.

 

In terms of your question around colours - Colour properties are complex values, not just text strings, so they don't translate to text like the other properties (which are mostly numeric and text). You can however use the JSON function to return the 8 character hex code that represents a colour property by converting it to a JSON value:

 

JSON(
     YourControl.Fill,
     JSONFormat.IgnoreUnsupportedTypes
)

 

In your case regarding the setting of a colour of an object, you maybe be able to set it by referring to the condition that will end up setting the colour.

i.e. if your button changes a value and you want the fill to change depending on the value, you can just write that code for checking the value into the Fill itself like:

Button1.Fill:

If( ControlName.Text = "Rejected", 
    //we can use css names for colours
    Red,
    ControlName.Text = "Accepted",
    // We can also refere to the hex colour
    ColorValue("#00FF00FF"),
    // and if there's no status
    // note we can also use RGBA() function 
    // as these colour are all interchangeable as colour objects
    RGBA(0, 0, 255, 1)
)

 

Cheers,

@iAm_ManCat


Please 'Mark as Solution' if someone's post answered your question and always 'Thumbs Up' the posts you like or that helped you!


Thanks!
You and everyone else in the community make it the awesome and welcoming place it is, keep your questions coming and make sure to 'like' anything that makes you 'Appy
Sancho Harker, MVP


Helpful resources

Announcements
October Events

Mark Your Calendars

So many events that are happening this month - don't miss out!

Ignite 2022

WHAT’S NEXT AT MICROSOFT IGNITE 2022

Explore the latest innovations, learn from product experts and partners, level up your skillset, and create connections from around the world.

Power Apps Africa Challenge 2022

Power Apps Africa Challenge

Your chance to join an engaging competition of Power Platform enthusiasts.

Top Solution Authors
Top Kudoed Authors
Users online (2,093)