Canvas Apps Controls Expressions

Weird combo box OnChange behavior – Should it trigger or not?

ComboBox OnChange behavior - Weird or not?
ComboBox OnChange behavior - Weird or not?

Combo box control

A combo box is used in Power Apps to present a list of items. It allows users to search for and select one or multiple items.  The two properties that we will focus on are (as defined on Microsoft Docs here):

  1. OnChange: How the app responds when the user changes a selection
  2. DefaultSelectedItems: The initial selected item(s) before the user interacts with the control

The weird OnChange behavior

To illustrate the behavior, I have a a very simple form. The form is set to display a record. One of the data cards of the form is a combo box. I am setting a variable to false on the app's OnStart property. I am setting the same variable to true on the combo box's OnChange property.

Now read the definitions of the two properties again. DefaultSelectedItems specifies the initial selected item(s) BEFORE the USER INTERACTS with the control. OnChange specifies how the app should respond when the USER CHANGES a SELECTION. Based on these definitions, my understanding or expected behavior is that the variable should stay false when the app loads. It should change to true only when the user changes the selected items of the combo box.

However, that isn't the actual behavior. Here's what happens:

Weird OnChange behavior of a combo box
Weird OnChange behavior of a combo box (click to enlarge)

As you can see, the OnChange gets triggered when the form gets populated with the record that it is expected to display. This doesn't seem to be in line with how these properties are defined. I am not suggesting that the actual behavior is incorrect. But I thought it is worth sharing with everyone since I have suffered because of this on one of my projects.

How I suffered because of this

I had a Patch on the OnChange of one of my combo boxes. The Patch was supposed to update an existing record which was stored in a variable. Every time I ran the app, the Patch would get executed even without me changing the selected values of that combo box. Since the variable that was supposed to store the record to be updated was blank, the Patch would end up creating blank records. So the end result was every time a user would run the app, there would be a blank record created. After spending hours, or may be days or even weeks, I was finally able to figure this out by using the monitor tool.

Here are the steps I followed to get past this:

  1. Set a variable to false on the app's OnStart
  2. Set that variable to true on the OnSelect of a control
  3. Change the OnChange of the combo box to be conditional on this variable. This way the OnChange wouldn't run until the user had actually interacted with the app.

Summary

To summarize, a combo box's OnChange gets triggered even if items get selected (based on DefaultSelectedItems) without any user interaction. This doesn't seem to be in line with the definitions of OnChange and DefaultSelectedItems properties. So set up the OnChange of your combo boxes carefully!

Recent articles

  1. Power Apps galleries – how to auto scroll!
  2. Make your Power Apps sessions persistent when switching Teams tab!
  3. How to display related records in a 1:N CDS relationship!

10 thoughts on “Weird combo box OnChange behavior – Should it trigger or not?”

  1. Hi Hardit,
    This one has caused me some grief as well and it seems to be something from a recent release as some code that was working fine until a couple of weeks ago suddenly produced different results (same code in two different apps). I am also finding that OnSelect gets triggered when the form opens, even if the control is not selected – example on a checkbox which I could use to toggle values, gets initiated to true (default false) on screen opening.
    I have worked around both of these, but would like to know if it is a bug that is going to be fixed.

  2. I ran into the same problem and added a variable (varUpdate = true) on the OnSelect of the combobox. After the update that’s triggered via the OnChange event of that combobox I would set it to false again. When the combobox is setup to accept multiple selections the OnChange could be called before the user interaction is completed. This would create wrong results:
    User tries to delete four records
    After clicking the ‘x’ twice (to delete selected items) the OnChange is fired and varUpdate is set to false.
    The following two user interactions won’t be handled since the OnChange is wrapped in an If(varUpdate, //DoUpdateStuff)

    Figured that it could be interesting to have this info alongside this article. Currently I only use comboboxes to show multiple selections, adding and deleting stuff is done with a popup which is activated by the OnSelect of that combobox and which uses two galleries (items & selected) so that every user interaction can be handled individually. The selected items are then visible in the combobox on the main screen.

  3. Hi,
    This issue stumped me for a good 30minutes, slightly embarrasing as a Power Platform Dev, But you are right and this seemingly goes against how the onchange property should work.
    But thanks for the advise, worked a treat!

Leave a Reply