Checkbox Question

Xojo Version 1.2 2024 Windows. This is kind of an armature question on the Checkbox Control. For trapping - I need to change the Checkbox value to false (unchecked) without triggering the code in the Checkbox Change Event. Both code examples below still trigger the Change Event code which I don’t want (Setting Off a Bug In My Program), I just want to change it’s state. Is it possible or is there a work around?

Checkbox1.Value = False
' Or
Checkbox1.VisualState = Checkbox.VisualStates.Unchecked

Thank You
Jeff

You can set a property somewhere that you can test in the ValueChanged event.

An example would be to add a property named ChangingInCode to the window. Use it like

ChangingInCode = true
Checkbox1.Value = false
ChangingInCode = false

In checkbox1.ValueChanged put

If ChangingInCode then return
' do the code for when the user clicks

You could make it tighter and subclass the checkbox and put all the code in the subclass. Then your checkbox1 code wouldn’t need to worry about it.

3 Likes

Thank You Tim

You should set Tim’s answer as solution, not your thank you. :wink:

2 Likes

An alternative if you don’t want to add additional properties is to use the Enabled state of the checkbox. Same concept, just using a built in property.

1 Like

It is possible to write this :

Sub ValueChanged()
If Me.Window.Focus = Me Then
’ do something
End If
End Sub

When posting code please highlight it and press the </> button. Thanks.

This issue/need comes up very very frequently. Xojo should add “User Action” and “Programmatic Action” as separate events to all controls. The existing Action event would then be “Any Action”.

I don’t think that’s necessary. When you need to know the source, there are multiple ways to flag to yourself that you can ignore the change.

I like to rely on the fact that the event occurs whenever it is the Value has Changed. That way I can do certain actions every time. Things like enable or disable other UI members based on the value state of a Checkbox. With this design, I’m not concerned with who or why the state is changing. I just rely on the behavior to be sure the other controls are enabled or disabled correctly.

Things that rely on knowing the change was not caused by code I can flag myself.

That’s not to dismiss your idea, I’m sure it is useful to you. It is just something I have not struggled with myself.

Very true. But those ways do add overhead to the work you’re doing.

As an aside, I have noticed that many of the MBS controls (e.g., DesktopNSSwitchControlMBS) do not trigger the Action or a change event when the control’s value has programmatically been changed. Very handy, that :slight_smile:

I would take @Julia_Truchsess 's suggestion and make it a parameter in the ValueChanged event called “ViaUIInteraction” or similar. This keeps compatibility with existing code while adding valuable context for those who need it.

1 Like

I maked a class for desktopCheckBox

c_DesktopCheckBox.xojo_binary_code.zip (1.4 KB)

2 Likes