Mega Code Archive

 
Categories / Delphi / VCL
 

How to check or uncheck a tcheckbox without causing an onclick event

Question: My application needs to update a few checkboxes and other controls but no events shall be fired by these changes. How can I prevent those OnClick() and other events? Answer: Before you update the control(s), you need to .. assign NIL to the event handler property for each control then update the control reassign the correct event handler - as shown in the code below. If several controls share one event handler, it would be more elegant to have a boolean flag which you temporarily set to false and the event handler(s) would check this flag before they do anything. procedure SetCheckBox(chk: TCheckBox; b: boolean); var NE: TNotifyEvent; begin { SetCheckBox } with chk do begin NE := OnClick; OnClick := nil; Checked := b; OnClick := NE end; { with chk } end; { SetCheckBox }