设置CheckBox.Checked没有OnClick事件

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.

TCheckBox Delphi控件显示一个可以打开(选中)或关闭(未选中)的复选框。 Checked属性指定是否选中复选框。

When the user clicks the checkbox to change its Checked state, the OnClick event for the checkbox is fired.

当用户单击复选框以更改其“已检查”状态时,将触发该复选框的OnClick事件。

更改复选框的选中属性 ( 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.

由于没有OnCheckedChanged事件,因此您可能会根据其OnClick事件中复选框的选中状态来处理程序逻辑。

However, if you programmatically change the Checked property, the OnClick event will be fired -- even though no user interaction took place.

但是,如果您以编程方式更改Checked属性,即使没有发生用户交互, 也会触发OnClick事件

There are (at least) two ways to programmatically change the checked property of the checkbox while "disabling" the OnClick event.

在“禁用” OnClick事件的同时,有(至少)两种方法可以通过编程方式更改复选框的选中属性。

删除OnClick处理程序,选中更改,放回原始的OnClick处理程序 ( 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 event's signature of a TCheckBox control is "type TNotifyEvent = procedure(Sender: TObject) of object;"

在Delphi for Win32中,一个事件只能附加一个事件处理程序(过程)(即使在Delphi for Win32中可以模拟多播事件)。 TCheckBox控件的OnClick事件签名是“对象的类型TNotifyEvent = procedure(Sender:TObject);”

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.

如果在更改复选框状态之前将NIL分配给OnClick事件,则恢复为原始的OnClick事件处理过程-将不会触发OnClick事件。

 procedure SetCheckedState(const checkBox : TCheckBox; const check : boolean) ;
var
   onClickHandler : TNotifyEvent;
 begin
   with checkBox do
   begin
     onClickHandler := OnClick;
     OnClick := nil;

    Checked := check;
已检查:=检查;
    OnClick := onClickHandler;
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.

上面的SetCheckedState切换CheckBox1复选框的Checked属性。

受保护的Hack:ClicksDisabled:= true ( 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.

当您以编程方式更改复选框的Checked属性时,另一种阻止OnClick执行的方法是利用“隐藏”(受保护的) ClicksDisabled属性。

By looking at the TCheckBox's SetState procedure which gets executed whenever the Checked property changes, the OnClick is fired if ClicksDisabled is not true.

通过查看每当Checked属性更改时都会执行的TCheckBox的SetState过程,如果ClicksDisabled不为true,则会触发OnClick。

Since ClicksDisabled is protected you cannot access it from your code.

由于ClicksDisabled受保护,因此无法从代码中访问它。

Luckily, the protected hack technique enables you to access those hidden/protected properties of a Delphi control.

幸运的是,受保护的黑客技术使您能够访问Delphi控件的那些隐藏/受保护的属性。

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.

您需要做的是声明一个简单的伪类,在将使用ClicksDisabled属性的同一单元中扩展TCheckBox。

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):

一旦您掌握了ClicksDisabled,只需将其设置为true,更改Checked属性,然后将ClicksDisabled设置回false(默认值):

 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.

注意:上面的代码使用受保护的ClicksDisabled属性切换名为“ CheckBox1”的复选框的Checked属性。

使用Delphi构建应用程序 ( Building Applications with Delphi )

翻译自: https://www.thoughtco.com/set-checkbox-checked-without-onclick-event-1057838

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值