Android Question How to prevent Checkbox CheckedChange event from triggering?

Widget

Well-Known Member
Licensed User
Longtime User
I have several checkboxes that I assign an initial value to them using code. But when I execute:

B4X:
cbXDisplayLabel.Checked = False

it will execute the cbxDisplayLabel_CheckedChange event. I don't want this event to trigger.

I thought I could disable cbxDisplayLabel but it has no effect, the event still triggers.

B4X:
cbXDisplayLabel.Enabled = False
cbXDisplayLabel.Checked = False   'cbxDisplayLabel_CheckedChange Event still triggers

I don't know why a disabled view will still trigger an event?


The only other alternative I could come up with is to assign a Tag=1 to each checkbox when I want to ignore the event and in the event check for the sender's tag and if =1 then ignore the event. But this seems hokey.

B4X:
cbXDisplayLabel.Tag        = 1
cbXDisplayLabel.Checked = False
cbXDisplayLabel.Tag        = 0

Is there a better solution?
TIA
 

Widget

Well-Known Member
Licensed User
Longtime User
You can set the initial value in the Designer.

That's right. But I may have to load the state of these 10 checkboxes from an outside source and I don't want to trigger events when I'm doing that. I'd rather trigger the event once when I'm finished assigning the values to the 10 checkboxes instead of triggering 10 events which are related to doing the same thing.

In Delphi I could have a block of code where I would save a control's OnChange event in a local variable, then set the control's OnChange event to nil so it won't be triggered. Then I can execute my code and it no longer triggers the event, then at the end of the block of code I would assign the saved Event back to the OnChange event. Works great. The only solution I've come up with is to use a Tag or perhaps a global variable to prevent the trigger from executing. Not the most elegant approach but it works.

Also, can you tell me why a disabled CheckBox still triggers an event when executing "cbxDisplayLabel.Checked = True"? The cbxDisplayLabel_CheckChanged event won't get triggered when I touch a disabled checkbox, but it will trigger the event if the value is set it using code. I thought if the view was disabled, I wouldn't be able to change its value even with code, and hence it would not execute the trigger. I was surprised to see a disabled view will still execute a trigger if code was sued. Strange. o_O

TIA
 
Upvote 0

Widget

Well-Known Member
Licensed User
Longtime User
I know that is a old post, but today i need this and i think i found a solution, so i want to share it. We can also use click event instead CheckedChange, so we can handler only event from user and change programmatically the value each time we want

Thanks. That will actually work. :p

I didn't know ToggleButton had a Click event because the Designer only generates "ToggleButton1_CheckedChange()" event. The help only shows a "CheckedChange(Checked As Boolean)" event and there is no mention of a Click event.

So if I put my user event code in ToggleButton1_Click it executes only if the user clicks on the button and won't get executed when the ToggleButton1.Checked = True gets executed located somewhere else in my code. Which is what I needed. Thanks.

Note: If the toggle button is clicked, the ToggleButton1_CheckChanged gets executed first then ToggleButton1_Click gets executed. So this one action executes 2 events. Strange but true. I'm not sure why the CheckChanged gets executed before the Click event. For future purposes, I thought there might be some way if the Click event gets executed first it could (somehow) conditionally prevent the CheckChanged event from getting called, so the Checked value does not change under some conditions controlled by the Click event. That won't be possible because the events are executed in the wrong order. Even if it did execute in the correct order: Click then CheckChanged, I'm not sure what the Click event could do to prevent CheckChanged from executing and changing the Checked value.
 
Upvote 0
Top