Saturday, January 4, 2020

Set CheckBox.Checked Without the OnClick Event

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 events 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 TCheckBoxs 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 Beginners Guide to Delphi Database Programming​​​​Integrating Basic Charts into Delphi ApplicationsHow to Move and Resize Controls at Run Time​Multithreaded Delphi Database Queries

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.