Checkbox Switch Statement

I have 4 checkboxes and a text field. If the user selects checkbox1, I want “1” to display in the text field. If the user selects checkbox2, I want “2” to display in the text field. And so on…

I have the following code written in the expression binding of the text field:


If I select checkbox1, I get “2” displayed in the text field and selecting the other checkboxes displays a “3” in the text field.

I am not sure what I am doing wrong here…

Thank you.

Good news :slight_smile:
You’ve reinvented the binEnum expression function:
You should be able to just switch to passing your four checkboxes to binEnum.

Thank you! I got the first part working.

Here’s my next question…
So what if a user selects checkbox1 and checkbox2, how can i get a value of say “5” displayed?

Let’s back up a bit (this seems like it might be an XY problem, of sorts).

What is your end goal with these checkboxes; what do they represent (in abstract terms) and what does the desired output represent?

Is there a truth table you can create of checkbox states → numeric output? Can this be boiled down a simple repeating pattern?

So I am creating a “service report” and the 4 checkboxes represent different types of equipment. When a user is filling out the service report, they select the checkbox(s) based on the equipment they worked on. The text field that is displaying the selected values, won’t be in the final design. Instead, I will just be writing the value, of the selected checkbox(s), to a parameter and then passing the parameter to a table which displays the information entered in the service report. I was only using the text field for development purposes, to get the functionality working.

Add a custom value property to each of the checkboxes. Name it whatever you want. key is the default and is fine. Set the value to the “weight” of that checkbox.

Check sum

Create an expression binding on the component to track the total.

{../Checkbox.custom.key} * {../Checkbox.props.selected}
 + {../Checkbox_0.custom.key} * {../Checkbox_0.props.selected} 
 + {../Checkbox_1.custom.key} * {../Checkbox_1.props.selected} 
 + {../Checkbox_2.custom.key} * {../Checkbox_2.props.selected}

So you’re encoding the parameter state, then unpacking it at the other end.

You could do that (unambiguously) using binEnums sibling function, binEnc - which packs a set of bits into binary, then returns the integer representation.

However, you really probably don’t need it. Parameters are free. I would just add parameters for each of these input parameters. It’s going to be a lot easier to reason about down the road.

1 Like