Title: Set the Checked property of a TCheckBox Delphi control Without raising the OnClick event
The TCheckBox Delphi control displays a check box that can be on (checked) or off (unchecked). The Checked property specifies whether the check box is checked or not.
When the user click the check box to change its Checked state, the OnClick event for the check box is fired.
How to Change the check box's Checked property Without raising the OnClick event?
Since there is no OnCheckedChanged event, you will probably handle the program logic dependant on the checked state of the check box in its OnClick event.
However, if you programmatically change the Checked property the OnClick event will be fired - even though no user iteraction took place.
There are (at least) two ways to programmatically change the checked property of the check box 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 check box, then revert to the original OnClick event handling procedure - the OnClick event will not be fired.
procedure SetCheckedState(const checkBox : TCheckBox; const check : boolean) ;
var
onClickHandler : TNotifyEvent;
begin
with checkBox do
begin
onClickHandler := OnClick;
OnClick := nil;
Checked := check;
OnClick := onClickHandler;
end;
end;
Usage of this procedure is simple:
//toggle Checked state
begin
SetCheckedState(CheckBox1, NOT CheckBox1.Checked) ;
end;
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 Check box, 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):
type
TCheckBoxEx = class(TCheckBox) ;
...
with TCheckBoxEx(CheckBox1) do
begin
ClicksDisabled := true;
Checked := NOT Checked;
ClicksDisabled := false;
end;
Note: the above code toggles the Checked property of the check box named "CheckBox1" using the protected ClicksDisabled property.