Set CheckBox.Checked Without the OnClick Event

Unhiding the ClicksDisabled Protected Property

Cursor over 'checked' checkbox on computer screen, close-up
Getty Images/David Gould

The TCheckBox Delphi control displays a checkbox that can be on (checked) or off (unchecked). The Checked property specifies whether the checkbox is checked or not.

When the user clicks the checkbox to change its Checked state, the OnClick event for the checkbox is fired.

Changing the Checkbox's Checked Property

Since there is no OnCheckedChanged event, you will probably handle the program logic dependent on the checked state of the checkbox in its OnClick event.

However, if you programmatically change the Checked property, the OnClick event will be fired -- even though no user interaction took place.

There are (at least) two ways to programmatically change the checked property of the checkbox while "disabling" the OnClick event.

Remove OnClick Handler, Change Checked, Put Back the Original OnClick handler

In Delphi for Win32, an event can have only one event handler (procedure) attached to it (even though there is a way to mimic multicast events in Delphi for Win32). The OnClick event's signature of a TCheckBox control is "type TNotifyEvent = procedure(Sender: TObject) of object;"

If you assign NIL to the OnClick event before you change the state of the checkbox, then revert to the original OnClick event handling procedure - the OnClick event will not be fired.

 procedure SetCheckedState(const checkBox : TCheckBox; const check : boolean) ;


   onClickHandler : TNotifyEvent;


   with checkBox do


     onClickHandler := OnClick;

     OnClick := nil;

    Checked := check;
    OnClick := onClickHandler;


Usage of this procedure is simple:

 //toggle Checked statebegin

   SetCheckedState(CheckBox1, NOT CheckBox1.Checked) ;


The SetCheckedState above toggles the Checked property of the CheckBox1 check box.

Protected Hack: ClicksDisabled: = true

Another way to stop the OnClick from executing, when you programmatically change the Checked property of a checkbox, is to take advantage of the "hidden" (protected) ClicksDisabled property.

By looking at the TCheckBox's SetState procedure which gets executed whenever the Checked property changes, the OnClick is fired if ClicksDisabled is not true.

Since ClicksDisabled is protected you cannot access it from your code.

Luckily, the protected hack technique enables you to access those hidden/protected properties of a Delphi control.

The accessing protected members of a component provides more info on the subject.

What you need to do is to declare a simple dummy class extending the TCheckBox in the same unit where you will use the ClicksDisabled property.

Once you get your hands on the ClicksDisabled, simply set it to true, change the Checked property, then set ClicksDisabled back to false (default value):



 TCheckBoxEx = class(TCheckBox) ;




 with TCheckBoxEx(CheckBox1) dobegin

   ClicksDisabled := true;

   Checked := NOT Checked;

   ClicksDisabled := false;


Note: the above code toggles the Checked property of the checkbox named "CheckBox1" using the protected ClicksDisabled property.

Building Applications with Delphi

mla apa chicago
Your Citation
Gajic, Zarko. "Set CheckBox.Checked Without the OnClick Event." ThoughtCo, Feb. 16, 2021, Gajic, Zarko. (2021, February 16). Set CheckBox.Checked Without the OnClick Event. Retrieved from Gajic, Zarko. "Set CheckBox.Checked Without the OnClick Event." ThoughtCo. (accessed June 8, 2023).