delphifiremonkeydelphi-10.1-berlintframe

Finding an embedded TFrame


I have a TFrame that is Inherited from a TBaseFrame = class(TFrame)

Inside this there is an embeded TFrame with same inheritence

  TViewStandardMovimentoFinanceiro = class(TFrameBase)
    ViewStandardEdiMovimentoFinanceiro1: TViewStandardEdiMovimentoFinanceiro;

  TViewStandardEdiMovimentoFinanceiro = class(TFrameBase)

  TFrameBase = class(TFrame, INaharView, INaharViewAdapter)

The TViewStandardMovimentoFinanceiro is created with parent set to the main form (particularly to a THorzScrollBox)

From INSIDE the TViewStandardMovimentoFinanceiro frame I tried the classical approach of using the Children list and have not found that embedded TFrame.

I have tried also using the Parent`s Children list with no success. Same thing with the Components List

What I want to do is to to locate all available TFrames so I can ask for an interface (I know how to do that)

What am I missing?

Following recommendations bellow I have implemented this code for testing:

    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;

When LocateControl is executed it goes several levels to the top and from there it tries to iterate on Children List, it only contains 3 items in a form form full of controls.


Solution

  • Your function LocateControl need to be recursive, as stated by Ondrej. Something like this

    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;