Skip to main content

Notifications

Community site session details

Community site session details

Session Id : 1KrNbTVqmnhOoDMFaGzYlq
Power Apps - Building Power Apps
Answered

Change the color of the button after it is clicked

Like (0) ShareShare
ReportReport
Posted on 6 Jul 2020 23:00:18 by 109

Hi,

 

I have a vertical gallery with buttons as one of the field's, So I have multiple buttons with one instance. Now I need to change the color of each button after it is clicked to show which buttons are clicked and which are not.

 

Thanks.

 

 

  • v-xida-msft Profile Picture
    on 13 Jul 2020 at 01:20:20
    Re: Change the color of the button after it is clicked

    Hi @MJK ,

    Have you taken a try with the solution I provided above? Is it helpful in your scenario?

     

    According to the needs that you mentioned, I think the solution I provided above could achieve your needs. Please consider take a try with it, check if the issue is solved.

     

    If you have solved your problem, please consider go ahead to click "Accept as Solution" to identify this thread has been solved.

     

    Best regards,

  • WarrenBelz Profile Picture
    146,745 Most Valuable Professional on 11 Jul 2020 at 23:17:35
    Re: Change the color of the button after it is clicked

    Hi @MJK ,

    Just checking if you got the result you were looking for on this thread. Happy to help further if not.

    Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.

  • WarrenBelz Profile Picture
    146,745 Most Valuable Professional on 08 Jul 2020 at 07:37:21
    Re: Change the color of the button after it is clicked

    Hi @MJK ,

    You need something to store the value in the gallery or you simply cannot control which buttons are to change colour. You can use AddColumns and set the Default to false, Patch true to it when the box is checked and bind the box to the added field.

     

    Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.

  • v-xida-msft Profile Picture
    on 08 Jul 2020 at 01:18:15
    Re: Change the color of the button after it is clicked

    Hi @MJK ,

    Based on the needs that you mentioned, I think the collection could achieve your needs. You could consider load your data source records into a collection, along with a new column "ButtonOptions" added via the AddColumns function.

     

    On your side, please consider set the OnStart property of App to following:

    ClearCollect(
     RecordsCollection,
     AddColumns(
     'Your Data Source',
     "ButtonOptions",
     ""
     )
    )

    them use the RecordsCollection as data source in your app instead of your original data source. And bind your Gallery to the RecordsCollection.

    Note: The result the AddColumns function returned is a temporary table, which would not make any changes to your underlying data source.

     

    After that, re-load your canvas app (fire the OnStart property of App), then try the solution I provided above, check if the issue is solved.

     

    Best regards,

  • MJK Profile Picture
    109 on 07 Jul 2020 at 22:45:27
    Re: Change the color of the button after it is clicked

    @WarrenBelz , @v-xida-msft 

    The Items property of my Gallery is a database table, How do I add an extra column to the Gallery without adding a column to the db table? 

  • WarrenBelz Profile Picture
    146,745 Most Valuable Professional on 07 Jul 2020 at 07:48:04
    Re: Change the color of the button after it is clicked

    Thanks @v-xida-msft for the huge answer to the small question.

    @MJK , please let me know if I have over-simplified this.

  • v-xida-msft Profile Picture
    on 07 Jul 2020 at 06:16:18
    Re: Change the color of the button after it is clicked

    Hi @MJK ,

    Could you please share a bit more about your app's configuration?

    Do you add several buttons inside your Gallery?

     

    Based on needs that you mentioned, I think you want to achieve your needs as following screenshot, is it right?

    6.JPG

     

    According to the needs that you mentioned, I think there is no direct way to achieve your needs. As an alternative solution, you need to add a additional column in your data source (your Gallery connected to) to store the button click status for each item in your Gallery.

     

    I have made a test on my side, please consider take a try with the following workaround:

    My data source structure as below:

    7.JPG

    Note: The ButtonOptions column is a used to store the button click status for each item in your Galleru.

     

    Add a Gallery in my app screen, set the Items property to above Table data source ("ProductsCollection"). Then add three buttons inside the Gallery, set the Text property of these buttons to "Option1", "Optjion2", "Option3", ... individually.

    Set the OnSelect property of the "Option1" button to following:

    Patch(
     ProductsCollection, 
     ThisItem, 
     {
     ButtonOptions: ThisItem.ButtonOptions & "Option1;" // Modify formula here for each button
     }
    )

    set the Fill property of the "Option1" button to following:

    If(
     "Option1" in ThisItem.ButtonOptions, // Modify formula here for each button
     RGBA( 255, 140, 0, 1 ), 
     RGBA(56, 96, 178, 1)
    )

     

    Set the OnSelect property of the "Option2" button to following:

     

    Patch(
     ProductsCollection, 
     ThisItem, 
     {
     ButtonOptions: ThisItem.ButtonOptions & "Option2;"
     }
    )

     

    set the Fill property of the "Option2" button to following:

     

    If(
     "Option2" in ThisItem.ButtonOptions, 
     RGBA( 255, 140, 0, 1 ), 
     RGBA(56, 96, 178, 1)
    )

     

     

    Set the OnSelect property of the "Option3" button to following:

     

    Patch(
     ProductsCollection, 
     ThisItem, 
     {
     ButtonOptions: ThisItem.ButtonOptions & "Option3;"
     }
    )

     

    set the Fill property of the "Option3" button to following:

     

    If(
     "Option3" in ThisItem.ButtonOptions, 
     RGBA( 255, 140, 0, 1 ), 
     RGBA(56, 96, 178, 1)
    )

    Please refer to above solution, then try it for your scenario, then check if the issue is solved.

    Test.gif

     

     

    Of course, if you want to unselect button option when you press button again, please consider modify the formula within the OnSelect property of each button in your Gallery as below:

    "Option1" OnSelect:

    If(
     "Option1" in ThisItem.ButtonOptions,
     Patch(
     ProductsCollection,
     ThisItem,
     {
     ButtonOptions: Substitute(ThisItem.ButtonOptions, "Option1;", "")
     }
     ),
     Patch(
     ProductsCollection, 
     ThisItem, 
     {
     ButtonOptions: ThisItem.ButtonOptions & "Option1;"
     }
     )
    )

     

    "Option2" OnSelect:

    If(
     "Option2" in ThisItem.ButtonOptions,
     Patch(
     ProductsCollection,
     ThisItem,
     {
     ButtonOptions: Substitute(ThisItem.ButtonOptions, "Option2;", "")
     }
     ),
     Patch(
     ProductsCollection, 
     ThisItem, 
     {
     ButtonOptions: ThisItem.ButtonOptions & "Option2;"
     }
     )
    )

    "Option3" OnSelect:

    If(
     "Option3" in ThisItem.ButtonOptions,
     Patch(
     ProductsCollection,
     ThisItem,
     {
     ButtonOptions: Substitute(ThisItem.ButtonOptions, "Option3;", "")
     }
     ),
     Patch(
     ProductsCollection, 
     ThisItem, 
     {
     ButtonOptions: ThisItem.ButtonOptions & "Option3;"
     }
     )
    )

    Please take a try with above solution, check if it could help in your scenario.

     

    Best regards,

  • Verified answer
    WarrenBelz Profile Picture
    146,745 Most Valuable Professional on 06 Jul 2020 at 23:25:42
    Re: Change the color of the button after it is clicked

    Hi @MJK ,

    You need to add a column to your gallery of true/false (boolean) type - I will call it BtnClick below.

    On the OnSelect of the button, put this

    Patch(
     ThisItem,
     {BtnClick:!BtnClick.Value}
    )

    Then the Colour of the button

    If(
     ThisItem.BtnClick=true,
     YourClickedColor,
     YourNormalColor
    )

     

    Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Understanding Microsoft Agents - Introductory Session

Confused about how agents work across the Microsoft ecosystem? Register today!

Warren Belz – Community Spotlight

We are honored to recognize Warren Belz as our May 2025 Community…

Congratulations to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 146,745 Most Valuable Professional

#2
RandyHayes Profile Picture

RandyHayes 76,287 Super User 2024 Season 1

#3
Pstork1 Profile Picture

Pstork1 66,091 Most Valuable Professional

Leaderboard