我想知道当我改变 CheckBox 的状态时
CheckBox->Checked=false;
它调用了CheckBox OnClick事件,如何避免它?
在较新的 Delphi 版本中,您可以使用类助手来添加此功能:
CheckBox.SetCheckedWithoutClick(False);
通过使用以下 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;
只是为了完整起见:FMXTCheckBox
会有类似的行为(触发OnChange
)。您可以使用以下类帮助器来解决此问题:
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;
免责声明:谢谢 dummzeuch 的原创想法。请注意有关类助手的常见提示。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)