Science, Tech, Math › Computer Science Set CheckBox.Checked Without the OnClick Event Unhiding the ClicksDisabled Protected Property Share Flipboard Email Print Getty Images/David Gould Computer Science Delphi Programming Delphi Tutorials Advanced PHP Programming Perl Python Java Programming Javascript Programming C & C++ Programming Ruby Programming Visual Basic View More By Zarko Gajic Zarko Gajic Twitter Computer Science Expert MSCS, Computer Science, University of Osijek Zarko Gajic is experienced in SQL and has working knowledge of DB systems such as MS SQL Server, Firebird, Interbase, and Oracle. He is also proficient in XML, DHTML, and JavaScript. Learn about our Editorial Process Updated on February 10, 2019 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) ;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 statebegin 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 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): type TCheckBoxEx = class(TCheckBox) ; ... with TCheckBoxEx(CheckBox1) dobegin ClicksDisabled := true; Checked := NOT Checked; ClicksDisabled := false; end; Note: the above code toggles the Checked property of the checkbox named "CheckBox1" using the protected ClicksDisabled property. Building Applications with Delphi Beginner's Guide to Delphi Database Programming Integrating Basic Charts into Delphi Applications How to Move and Resize Controls at Run Time Multithreaded Delphi Database Queries Cite this Article Format mla apa chicago Your Citation Gajic, Zarko. "Set CheckBox.Checked Without the OnClick Event." ThoughtCo, Feb. 16, 2021, thoughtco.com/set-checkbox-checked-without-onclick-event-1057838. Gajic, Zarko. (2021, February 16). Set CheckBox.Checked Without the OnClick Event. Retrieved from https://www.thoughtco.com/set-checkbox-checked-without-onclick-event-1057838 Gajic, Zarko. "Set CheckBox.Checked Without the OnClick Event." ThoughtCo. https://www.thoughtco.com/set-checkbox-checked-without-onclick-event-1057838 (accessed June 8, 2023). copy citation