如何解决如何知道TButton单击是否执行了TAction?
我在VCL应用程序中使用TAction和TButton。将“ TButtons操作”字段设置为现有操作可集中代码。
动作Execute方法如下:
void __fastcall MyFrame::MyActionExecute(TObject *Sender)
{
//Some action code
}
将MyAction分配给名为MyBtn的TButton并查看操作 ActionComponent :
void __fastcall MyFrame::MyActionExecute(TObject *Sender)
{
if(MyAction->ActionComponent == MyBtn)
{
//.. action code when the MyBtn was clicked..
}
}
似乎工作良好。
但是,以编程方式调用MyAction的Execute方法 ,如下所示:
MyActionExcecute(NULL);
似乎没有将ActionComponent设置为NULL,而是“仍然”使用MyBtn作为ActionCompoent。因此,即使未单击按钮,上面的if语句的评估结果也为true。
问题是,处理按钮单击和手动调用动作执行方法的正确方法是什么?
我知道我可以检查Sender参数是否为NULL,如果是,我可以假定它不是按钮。
解决方法
仅当UI控件通过内部为自己创建的内部ActionComponent
对象触发操作时,才设置操作的TBasicActionLink
属性。链接的Execute()
方法具有一个AComponent
参数,控件将其Self
/ this
指针传递给该参数,以在调用操作的{{ 1}}方法。
例如,这是VCL内部执行的操作:
ActionComponent
Execute()
procedure TControl.SetAction(Value: TBasicAction);
begin
if Value = nil then
begin
ActionLink.Free;
ActionLink := nil;
...
end
else
begin
...
if ActionLink = nil then
ActionLink := GetActionLinkClass.Create(Self);
ActionLink.Action := Value;
...
end;
end;
因此,请勿直接调用您的procedure TControl.Click;
begin
{ Call OnClick if assigned and not equal to associated action's OnExecute.
If associated action's OnExecute assigned then call it,otherwise,call
OnClick. }
if Assigned(FOnClick) and (Action <> nil) and not DelegatesEqual(@FOnClick,@Action.OnExecute) then
FOnClick(Self)
else if not (csDesigning in ComponentState) and (ActionLink <> nil) then
ActionLink.Execute(Self) // <-- HERE
else if Assigned(FOnClick) then
FOnClick(Self);
end;
事件处理程序 。那根本不会更新动作。而是调用操作的Execute()
方法。您只需要事先将操作的function TBasicActionLink.Execute(AComponent: TComponent): Boolean;
begin
FAction.ActionComponent := AComponent; // <-- HERE
Result := FAction.Execute;
end;
设置为NULL,例如:
OnExecute
当用户单击客户端控件时,该客户端会在调用操作的
ActionComponent
方法之前设置MyAction->ActionComponent = NULL; MyAction->Execute();
。 执行该操作后,该操作会将ActionComponent
重置为nil(Delphi)或NULL(C ++)。
但是,Execute
不会自动重置,只有在UI控件下次决定自行执行该操作时才可以。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。