Delphi-如何调用ActionList按钮点击? [英] Delphi- How to Call ActionList on button click?

查看:594
本文介绍了Delphi-如何调用ActionList按钮点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Delphi XE8中制作了一个多设备应用程序,它使用LiveBindings进行数据集。



有一些针对FMX的特定于LB的操作,包括TFMXBindNavigateDelete我试图用这样的按钮点击处理程序,如下所示:



按钮点击代码

  procedure TForm1.Button1Click(Sender:TObject); 
begin
如果cdsOrdersSTATUS.Value ='READY'然后
begin
ShowMessage('您的订单已经准备好/待命!
end
else
begin
TAction(ActionList1.Actions [0])。执行; //不工作,为什么?
结束
结束

ActionList1的Actions中的第一个(而且仅)项是我的FMXBindNavigateDelete1。



问题是,即使代码 TAction(ActionList1.Actions [0])。执行执行,当前数据集记录是删除,所以显然
TFMXBindNavigateDelete的Action没有任何效果。为什么会这样,我该怎么办?



Pic。 ActionList1:



< img src =https://i.stack.imgur.com/Q1f3K.jpgalt =ActionList>

解决方案

实际上,我认为这是一个很好的问题,不值得推荐。



我可以重现你的问题。我在FMX表单上放了两个按钮。我设置
Button1的OnClick到您的 Button1Click 和Button2的 Action LiveBindingsBindNavigateDelete1



点击按钮2会弹出标准的删除记录?确认,如果我回答是,则删除当前记录
,如但是,当点击Button1时,即使您执行了 else 块,删除记录确认
出现,所以记录没有被删除的机会。



原因是在代码

 函数TCustomAction.Execute:Boolean; 
begin
结果:= False;
如果支持但未暂停,则
begin
更新;
如果Enabled和AutoCheck然后
if(not Checked)或(Checked and(GroupIndex = 0))然后
已选中:=未选中;
如果启用然后
结果:=((ActionList<> nil)和ActionList.ExecuteAction(Self))或
((Application<> nil))和Application.ExecuteAction自身))或继承的Execute或
((Application<> nil)和Application.ActionExecuteTarget(Self));
结束
结束

默认情况下,启用属性似乎为在
更新的调用期间设置为False,所以如果启用,那么将不会执行。在$ code>更新的调用过程中,我没有设法找到
的方式来获取启用设置为True。也许别人知道如何做到这一点。



Button2 的情况下,执行然后传递到 TComponent.ExecuteAction
它调用$ code Action.ExecuteTarget(Self)
在其中导致
记录删除例程执行。



所以,从这个,你的问题似乎让我成为如何调整代码,使
TComponent.ExecuteAction 被执行,换句话说,如何将
Action 与组件相关联。答案是相当明显的。



所需要的是这个

 程序TForm1.Button1Click(Sender:TObject); 
begin
如果cdsOrdersSTATUS.Value ='READY'然后
begin
ShowMessage('您的订单已经准备好/待命!
end
else
begin
Button1.ExecuteAction(LiveBindingsBindNavigateDelete1); //< - 这个工程
//LiveBindingsBindNavigateDelete1.Execute; //不工作,为什么?
结束
结束


I'm making a multi-device application in Delphi XE8 which uses LiveBindings to a dataset.

There are a number of LB-specific Actions for FMX, including TFMXBindNavigateDelete. I'm trying to use this in a button-click handler like this:

Button Click Code:

procedure TForm1.Button1Click(Sender: TObject);
begin
  if cdsOrdersSTATUS.Value='READY' then
  begin
    ShowMessage('Your Order Is Already READY/PENDING!');
  end
  else
  begin
    TAction(ActionList1.Actions[0]).Execute; //Not working,why?
  end;
end;

The first (and only) item in ActionList1's Actions is my FMXBindNavigateDelete1.

The problem is, even if the code TAction(ActionList1.Actions[0]).Execute executes, the current dataset record is not deleted, so apparently TFMXBindNavigateDelete's Action has no effect. Why is this, and how can I make it work?

Pic. ActionList1:

解决方案

Actually, I think this is a good question and doesn't deserve the downvote.

I can reproduce your problem. I put two buttons on the FMX form. I set Button1's OnClick to your Button1Click and Button2's Action to LiveBindingsBindNavigateDelete1.

Clicking Button2 pops up the standard 'Delete record?' confirmation and deletes the current record if I answer "Yes", as expected.

However, when clicking Button1, even if your else block executes, the 'Delete record?' confirmation does not appear, so the record has no chance of being deleted.

The reason is in the code

function TCustomAction.Execute: Boolean;
begin
  Result := False;
  if Supported and not Suspended then
  begin
    Update;
    if Enabled and AutoCheck then
      if (not Checked) or (Checked and (GroupIndex = 0)) then
        Checked := not Checked;
    if Enabled then
      Result := ((ActionList <> nil) and ActionList.ExecuteAction(Self)) or
        ((Application <> nil) and Application.ExecuteAction(Self)) or inherited Execute or
        ((Application <> nil) and Application.ActionExecuteTarget(Self));
  end;
end;

The Enabled property seems by default to be set to False during the call to Update so the if Enabled then ... never executes. I haven't managed to find a way to get Enabled set to True during the call to Update. Perhaps someone else knows how to do that.

In the case of Button2, execution then passes to TComponent.ExecuteAction and it is the call to Action.ExecuteTarget(Self) in it which results in the record-deletion routine executing.

So, from that, your problem seemed to me to become how to adjust the code so that TComponent.ExecuteAction gets executed, in other words, how to associate the Action with a component. The answer was fairly obvious.

All that's needed is this

procedure TForm1.Button1Click(Sender: TObject);
begin
 if cdsOrdersSTATUS.Value='READY' then
  begin
    ShowMessage('Your Order Is Already READY/PENDING!');
  end
  else
  begin
    Button1.ExecuteAction(LiveBindingsBindNavigateDelete1);  //  <- this works
    //LiveBindingsBindNavigateDelete1.Execute; //Not working,why?
  end;
end;

这篇关于Delphi-如何调用ActionList按钮点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆