I\'m wondering so when I change state of CheckBox
CheckBox->Checked=false;
It calls CheckBoxOnClick Event , how to avoid it ?
In newer Delphi versions you can use class helpers to add this functionality:
CheckBox.SetCheckedWithoutClick(False);
by using the following class helper for a VCL TCheckBox
:
TCheckBoxHelper = class helper for TCheckBox
procedure SetCheckedWithoutClick(AChecked: Boolean);
end;
procedure TCheckBoxHelper.SetCheckedWithoutClick(AChecked: Boolean);
begin
ClicksDisabled := True;
try
Checked := AChecked;
finally
ClicksDisabled := False;
end;
end;
Just for completeness: A FMX TCheckBox
will behave similar (triggering OnChange
). You can workaround this by using the following class helper:
TCheckBoxHelper = class helper for TCheckBox
procedure SetCheckedWithoutClick(AChecked: Boolean);
end;
procedure TCheckBoxHelper.SetCheckedWithoutClick(AChecked: Boolean);
var
BckEvent: TNotifyEvent;
begin
BckEvent := OnChange;
OnChange := nil;
try
IsChecked := AChecked;
finally
OnChange := BckEvent;
end;
end;
Disclaimer: Thanks, dummzeuch for the original idea. Be aware of the usual hints regarding class helpers.