查找嵌入式TFrame

时间:2018-08-15 14:16:15

标签: delphi firemonkey delphi-10.1-berlin

我有一个继承自TBaseFrame = class(TFrame)的TFrame

在其中有一个具有相同继承关系的嵌入式TFrame

  TViewStandardMovimentoFinanceiro = class(TFrameBase)
    ViewStandardEdiMovimentoFinanceiro1: TViewStandardEdiMovimentoFinanceiro;

  TViewStandardEdiMovimentoFinanceiro = class(TFrameBase)

  TFrameBase = class(TFrame, INaharView, INaharViewAdapter)

创建TViewStandardMovimentoFinanceiro时将父级设置为主要形式(尤其是THorzScrollBox)

在INSIDE的TViewStandardMovimentoFinanceiro框架中,我尝试了使用Children列表的经典方法,但没有发现嵌入的TFrame。

我也尝试过使用“父母的孩子”列表,但没有成功。与组件列表相同

我要做的是找到所有可用的TFrame,以便我可以请求一个接口(我知道该怎么做)

我想念什么?

以下建议如下,我已实现以下代码进行测试:

    function TFrameBase.LocateControl(AControl: TControl; ADomainName: string): TControlHandler;
var
  NaharView: INaharView;
  ControlHandler: TControlHandler;
  i: integer;
begin
  result := nil;
  for i := 0 to AControl.ChildrenCount - 1 do
    if (AControl.Children[i] is TFrame) and (AControl.Children[i] <> Self) then
      if Supports((AControl.Children[i] as TFrame), INaharView, NaharView) then
      begin
        ControlHandler := NaharView.Control[ADomainName];
        if Assigned(ControlHandler) then
          exit(ControlHandler);
      end;
end;

function GetUltimateParent(Control: TControl): TControl;
begin
  if Control.Parent is TControl then
    Result := GetUltimateParent(TControl(Control.Parent))
  else
    Result := Control;
end;

function TFrameBase.GetNaharControl(ADomainName: string): TControlHandler;
var
  i: integer;
  ControlHandler: TControlHandler;
begin
  if NaharControls.ContainsKey(ADomainName) then
    Exit(NaharControls.Items[ADomainName])
  else
  begin
    ControlHandler := LocateControl(GetUltimateParent(Self), ADomainName);
    if Assigned(ControlHandler) then
      exit(ControlHandler);
  end;

  raise EViewControlDomainNameNotFound.Create(ADomainName);
end;

执行LocateControl时,它会跳到最上层,并尝试从那里迭代到子级列表上,它仅包含3个项目,并且形式完整,包含控件。

1 个答案:

答案 0 :(得分:1)

如Ondrej所述,您的函数LocateControl必须是递归的。像这样

function TFrameBase.LocateControl(AControl: TControl; ADomainName: string): TControlHandler;
var
  NaharView: INaharView;
  ControlHandler: TControlHandler;
  i: integer;
begin
  result := nil;
  for i := 0 to AControl.ChildrenCount - 1 do
  begin
    if (AControl.Children[i] is TFrame) and (AControl.Children[i] <> Self) then
    begin
      if Supports((AControl.Children[i] as TFrame), INaharView, NaharView) then
      begin
        ControlHandler := NaharView.Control[ADomainName];
        if Assigned(ControlHandler) then
        begin
          exit(ControlHandler);
        end;
      end;
    end;
    // recursive bit
    Result := LocateControl(AControl.Children[i], ADomainName):
    if assigned( Result ) then
    begin
      exit;
    end;
  end;
end;