Mega Code Archive

 
Categories / Delphi / VCL
 

Change the value of a CheckBox without triggering the OnClick event handler

Title: Change the value of a CheckBox without triggering the OnClick event handler Question: When initializing the Checked property of a CheckBox, the OnClick event handler will be triggered. This solution will present a unit, that does not call the event handler. Answer: .general1-comment { font-style: italic; } .general1-identifier { } .general1-number { } .general1-preprocessor { } .general1-reservedword { font-weight: bold; } .general1-space { } .general1-string { } .general1-symbol { } The Problem Often one has assigned an OnClick event handler to the CheckBox. When initializing an application and setting the Checked property, usually Delphi will call the OnClick event handler, which often is unwanted when just initializing a form. The Usual Solution Usually a programmer will temporarly set the event handler to nil, set the Checked value and then reset the event handler. Personally, I dislike this way, so I want to present you another solution to this problem. My Solution I have create a new unit, that will give you three procedures allowing you to manipulate the State/Value of a check box. procedure ToggleCheckBoxSilent(aCheckBox: TCustomCheckBox); procedure SetCheckBoxSilent(aCheckBox: TCustomCheckBox; aValue: Boolean); procedure SetCheckBoxStateSilent(aCheckBox: TCustomCheckBox; aState: TCheckBoxState); ToggleCheckBoxSilent toggles the Checked value of a check box from true to false and vice versa SetCheckBoxSilent allows you to explicitly set the value of a check box SetCheckBoxStateSilent allows you to set the state of a check box The Implementation Of The Solution The TCustomCheckBox has a protected property named ClicksDisabled, which when set to True will supress the OnClick event handler. However, since TCustomCheckBox protects this property we will have to create a helper class, that will allow us to access this property through a type cast. type THiddenCheckBox = class(TCustomCheckBox) end; This allows us, to access the property as needed. The Source Code Following the full source code of the unit. Simply add the unit to the uses statement of your form and use the procedures as needed. unit uSetCheckBoxSilent; interface uses StdCtrls; procedure ToggleCheckBoxSilent(aCheckBox: TCustomCheckBox); procedure SetCheckBoxSilent(aCheckBox: TCustomCheckBox; aValue: Boolean); procedure SetCheckBoxStateSilent(aCheckBox: TCustomCheckBox; aState: TCheckBoxState); implementation type THiddenCheckBox = class(TCustomCheckBox) end; procedure ToggleCheckBoxSilent(aCheckBox: TCustomCheckBox); begin SetCheckBoxSilent(aCheckBox, not THiddenCheckBox(aCheckBox).Checked); end; procedure SetCheckBoxSilent(aCheckBox: TCustomCheckBox; aValue: Boolean); var OldClick: Boolean; begin OldClick := THiddenCheckBox(aCheckBox).ClicksDisabled; THiddenCheckBox(aCheckBox).ClicksDisabled := True; THiddenCheckBox(aCheckBox).Checked := aValue; THiddenCheckBox(aCheckBox).ClicksDisabled := OldClick; end; procedure SetCheckBoxStateSilent(aCheckBox: TCustomCheckBox; aState: TCheckBoxState); var OldClick: Boolean; begin OldClick := THiddenCheckBox(aCheckBox).ClicksDisabled; THiddenCheckBox(aCheckBox).ClicksDisabled := True; THiddenCheckBox(aCheckBox).State := aState; THiddenCheckBox(aCheckBox).ClicksDisabled := OldClick; end; end. Daniel Wischnewski