Checkbox action only on click

The Action event of a checkbox is triggered by the user clicking on the control or by changing the value of the control programmatically.
What I want is to have the code of the Action event only triggered by clicking on the control.

For example, when the window initially opens, I want to set up the state of all the checkboxes and do not want them activating their Action events.

What is the best way to do this? Why isn’t there an event (like Clicked) that only is triggered by the user actually clicking on the control?

Should I be using the MouseDown/MouseUp events?

dont use the label of the check box
make it really just the size of the checkbox & use a label next to the checkbox

Yes. Use a flag such as HasBeenClicked as Boolean you set in MouseDown, and set it back to false in the checkBox Action event.

So only when hasBeenClicked is true in Action, you know it was an actual click and not a change in code.

This may be a silly question, but is the mouse down event guaranteed to come prior to the changed state (action) event? I like the solution, but was wondering if there could be a corner case where the sequence of events is not guaranteed.

another trick I use is to add

if me.enabled then end if

in the Action event

And if I wan to change the value in code, use

ctrl.enabled = false ctrl.value =1 ctrl.enabled = true

Yes, Action occurs on MouseUp.

Thanks for the suggestions. First of all the confirmation that some kind of contortion was going to be required. Both the Bujardet and the Tullin recommendations seem doable.

[quote]dont use the label of the check box
make it really just the size of the checkbox & use a label next to the checkbox[/quote]

This reply I did not understand. I am not sure what this would accomplish. If you reduce the size of the click target, does that really change anything?

never mind I misread

Perhaps a small point,

Should this statement be more accurately: Action occurs after MouseDown?

The way that I am doing it is Returning False in the MouseDown so the MouseUp should not occur. Right?

By definition, MouseUp cannot take place before MouseDown has occurred. In essence, YES, ACTION ALWAYS TAKES PLACE AFTER MOUSEDOWN.

Then won’t you simply shove

System.DebugLog currentMedthodName

in each event and see for yourself. At one point you got to be proactive as well.

All you’re doing is getting more and more confused, instead of coding. Get to your keyboard instead of arguing.

No. Use the MouseUp event. Check that the mouse was released inside the control before triggering your action.

A common fix for mis-clicking something is to drag the mouse outside of the object you didn’t want to click and release outside it.

Action and MouseUp are doing just the same in this instance. Why second guess the built-in event ?

[quote=263093:@Robert Livingston]The Action event of a checkbox is triggered by the user clicking on the control or by changing the value of the control programmatically.
What I want is to have the code of the Action event only triggered by clicking on the control.
[/quote]
This sounds attractive BUT I think you’re going to find doing this causes you other headaches down the road
Like “why when I change the value of the check box does the action NOT run?” - which you might expect because thats “normal”
I’d make it so the checkbox, or subclass preferably, has a flag, DontDoAction, you can toggle so it knows to perform the action or not
That way at app open or window open you can toggle all those flags OFF, set up the UI the way you want, toggle them back on and everything now works like normal
And you can even reveal that in the inspector so you can set their initial state right in the IDE

That way you can toggle whether a control does / does not run its action at any point - in code or not

For Checkbox? In the original question, changing the value of Checkbox by code will trigger it’s Action event - sometimes this behavior is not desired. MouseUp is only triggered by the mouse, so if you want a change-by-click only action, you would need to track MouseDown, and verify the MouseUp occurred inside the control to mimic the Action event.

I originally read the original post as “I want make the check box click ONLY when you click the checkbox itself & not the label”
Hence my original post

Rereading it I then get the sense that, like many of us, Robert wanted to sometimes NOT perform the action - like when loading a window with a bunch of values, and subsequently would then like it to react “as normal”
Therefore my post about a means to sometimes ignore the event and then return to normal after that set up is done

If you want a “click only” checkbox that NEVER runs its action by code then Tim’s suggestion should work but you do have to track that mouse up happened in the control as he notes

Which is exactly what the flag HasBeenClicked tracks, so one can discriminate between actual click and code. But hey, there are several ways to obtain what the OP wants. They may have all been described. We have been thorough.

I apologize for being obtuse.

The reason that I was using the MouseDown event to set the flag rather than the MouseUp event is that to get to the MouseUp event requires that the MouseDown event return True. That has two consequences.

  1. If the MouseDown event returns True, then the Action Event (if any) will not execute. So your code cannot be in the Action event. If you move the code to the MouseUp event then the desired code will run, but I still have a problem. I want the state of the checkbox to change. (see number 2)

  2. If the MouseDown event returns True, then the state of the checkbox will not change.

If you’re tracking and calling your own Action, then #1 is not a problem.

#2 is easy enough to handle. Before performing your custom action code (which I highly recommend you put in a separate method,) add this line: me.value = not me.value

That is the easiest way to do it and then use code in Action to deal with the flag. Using MouseUp is more complicated.

Create a Class called MyCheckBox, set its Super to CheckBox
Create a Property called TriggerAction as a Private Boolean under MyCheckBox
Create a Computed Property called ValueQ (stands for value quiet, call it what you want) as a Boolean
Under Set enter the following code:

TriggerAction = False me.Value = value TriggerAction = True
Under Get enter the following code:

Return me.Value

Under the Action event enter the following code:

if TriggerAction then Action end if
Add an Event Definition for Action
Repeat for State if you use that.

Change the Super for your existing checkboxes to MyCheckBox and they will all work as normal, but you have a new Property called ValueQ which you can use to set its state without firing the event.

Sample project link