Skip to main content

Notifications

Community site session details

Community site session details

Session Id : gS6no8oQU7axabmbHnwZW7
Power Apps - Building Power Apps
Answered

How to assign default value to combobox control without triggereing its onchange event?

Like (0) ShareShare
ReportReport
Posted on 10 Nov 2021 08:01:35 by 249

OnChange of Combobox is triggered when DefaultSelectedItems is assigned some value. Unlike dropdown control.  How to assign default value to combobox control without triggereing its onchange event?

 

Detailed Explanation

 

2.png

Btn_Initialize

OnSelect

 

ClearCollect(
 GalData,
 Table(
 {
 CmBx1:{Id:1,Value:"Item1_1"},
 CmBx2:{Id:1,Value:"Item2_1"},
 CmBx3:{Id:1,Value:"Item3_1"}
 },
 {
 CmBx1:{Id:2,Value:"Item1_2"},
 CmBx2:{Id:2,Value:"Item2_2"},
 CmBx3:{Id:2,Value:"Item3_2"}
 }
 )
);

 

 

Collection Gallery & Selection Gallery

Items 

 

GalData

 

 

ComboBox1

Items

 

Table(
 {Id:1, Value:"Item 1_1"},
 {Id:2, Value:"Item 1_2"},
 {Id:3, Value:"Item 1_3"},
 {Id:4, Value:"Item 1_4"},
 {Id:5, Value:"Item 1_5"},
 {Id:6, Value:"Item 1_6"},
 {Id:7, Value:"Item 1_7"},
 {Id:8, Value:"Item 1_8"},
 {Id:9, Value:"Item 1_9"},
 {Id:10, Value:"Item 1_10"}
)

 

 

DefaultSelectedItems

 

ThisItem.CmBx1

 

SelectMultiple

 

false

 

 

OnChange

 

Patch(
GalData, LookUp(GalData,CmBx1.Id=ThisItem.CmBx1.Id),
{
CmBx2:{Id: -1,Value: Blank()},
CmBx3:{Id: -1,Value: Blank()}
}
)

 

 

ISSUE

I'm having issue with this OnChange in ComboBox Control.

  • When the gallery is initialised the onchange is triggered and subsequent combobox value is empty.
  • I wanted to update the datasource as the combobox is changed.

The same can be achieved using dropdown control without any issue, why Combobox is behaving abruptly?

Categories:
  • Verified answer
    WarrenBelz Profile Picture
    146,651 Most Valuable Professional on 10 Nov 2021 at 21:09:36
    Re: How to assign default value to combobox control without triggereing its onchange event?

    @pcakhilnadh ,

    I was answering your question on unexpected behaviour, which I have also experienced and I believe is a bug (therefore I cannot explain the reason). My "solution" is (as I noted) a workaround that you really should not have to do, but is the only one I can think of. My personal solution has been to use Drop-Downs instead of Combo Boxes if I want OnChange triggered directly from Edit mode from them (which I need rarely in my designs). You can also consider presenting the user with View mode and set the Variable on the button that puts the form in Edit mode.

     

    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.

    Visit my blog Practical Power Apps

  • pcakhilnadh Profile Picture
    249 on 10 Nov 2021 at 12:51:28
    Re: How to assign default value to combobox control without triggereing its onchange event?

    @WarrenBelz  According to your solution,

    Screen3

    • OnVisible

     

    UpdateContext({varPatchNow:false})

     

    ComboBox control

    • OnSelect

     

    UpdateContext({varPatchNow:true})

     

    • OnChange

     

    If(
     varPatchNow,
     Patch(
     GalData, 
     {CmBx1.Id:ThisItem.CmBx1.Id},
     {
     CmBx2:
     {
     Id: -1,
     Value: Blank()
     },
     CmBx3:
     {
     Id: -1,
     Value: Blank()
     }
     }
     )
    );
    
    UpdateContext({varPatchNow:false});

     

     

    Which is showing unexpected behaviour

    • When user go to screen3 'varPatchNow' is set False   - OK behavior
    • When a user clicks on Combobox1 'varPatchNow' is set True - OK behavior
    • After onchange is completed  'varPatchNow' is set False - OK behavior
    • After which 'varPatchNow' is being set as True and False which is clearing the data.  I don't understand this behavior.

    Please explain why it is so...?


    A Solution I found.

    • Removed OnSelect property of ComboBox1 
      UpdateContext({varPatchNow:true})​
    • Added an icon next to ComboBox and added this function to its onselect

    Now Combo Box works fine but overhead is that if I want to change a value then everytime I need to click on icon next to it

    Looking for solution without this icon and extra click

  • WarrenBelz Profile Picture
    146,651 Most Valuable Professional on 10 Nov 2021 at 10:38:42
    Re: How to assign default value to combobox control without triggereing its onchange event?

    @pcakhilnadh ,

    You are doing this - my code simply stops the event triggering when the form loads (which is the question you posted The same can be achieved using dropdown control without any issue, why Combobox is behaving abruptly ). You need to set the Variable to true after it loads (probably present the Form in View mode than put the code on the Edit button) and then the OnChange event will work when the combo box changes - as I said, this is a workaround for the combo box behaviour that you would not expect.

     

    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.

    Visit my blog Practical Power Apps

  • pcakhilnadh Profile Picture
    249 on 10 Nov 2021 at 10:32:30
    Re: How to assign default value to combobox control without triggereing its onchange event?

    @WarrenBelz  I wanted to patch during "OnChange" which is possible using dropdown control. Please tell me how to patch onChange of combo box control.

     

    I wanted to patch onchange of combobox control blank value to 'Cmbx2' and 'Cmbx3' 

    Please tell how to do this ?

  • WarrenBelz Profile Picture
    146,651 Most Valuable Professional on 10 Nov 2021 at 09:53:03
    Re: How to assign default value to combobox control without triggereing its onchange event?

    Hi @pcakhilnadh ,

    Answer is still the same - set a Variable at Screen OnVisible

    UpdateContext({varPatchNow:false})

    Combo Box OnChange

    If(
     varPatchNow,
     Patch(
     GalData, 
     {CmBx1.Id:ThisItem.CmBx1.Id},
     {
     CmBx2:
     {
     Id: -1,
     Value: Blank()
     },
     CmBx3:
     {
     Id: -1,
     Value: Blank()
     }
     }
     )
    )

    then when you want to actually Edit the Item

    UpdateContext({varPatchNow:true})
    

    Note this is a complete workaround, but it is the only way I know to stop this behaviour.

     

    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.

    Visit my blog Practical Power Apps

     

  • pcakhilnadh Profile Picture
    249 on 10 Nov 2021 at 09:00:19
    Re: How to assign default value to combobox control without triggereing its onchange event?

    @WarrenBelz  I've found the root cause of the issue and modified the question ( I didn;t find a way to delete the question) Can you please check the question again

  • WarrenBelz Profile Picture
    146,651 Most Valuable Professional on 10 Nov 2021 at 08:56:16
    Re: How to assign default value to combobox control without triggereing its onchange event?

    Hi @pcakhilnadh 

    Yes - that is a nasty habit it has when a Form is opened - you may have to conditionally trigger the OnChange on a Variable and set this when you want it to Edit the form.

     

    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.

    Visit my blog Practical Power Apps

     

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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Markus Franz – Community Spotlight

We are honored to recognize Markus Franz as our April 2025 Community…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 146,651 Most Valuable Professional

#2
RandyHayes Profile Picture

RandyHayes 76,287 Super User 2024 Season 1

#3
Pstork1 Profile Picture

Pstork1 65,999 Most Valuable Professional

Leaderboard
Loading started