How to get structure of the hierarchy of a delphi component?

1

Personal I'm trying to refine my error log and an idea came up for me.

Using a ApplicationEvents component

I have a routine that writes the error log, in it I get the error in E.Message , and the Sender component that generated the error, so it happens, sometimes a Objeto does not have the Name property and yes ClassName, in this case it is difficult to identify where the error occurred, to improve it I capture the Form that is Active with " Screen.ActiveControl.UnitName ", but sometimes the active form is not the one that generated the error but rather one that is being created by it ... in that case I would have to get the Component that generated the error and go up the hierarchy, so I would know what course I should do to simulate the development error.

It also has a function coupled to this routine that captures the screen at the time of the error, but sometimes the delay that exists does not capture exactly where the mouse was, since it can be in a menu that has already closed.

That is, in short I need to go through the hierarchy of an Object, how can I do this?

Complementing because I need the hierarchy, because sometimes a component is inside a Panel->TabSheet->PageControl->Panel->Form .

    
asked by anonymous 09.05.2018 / 19:06

1 answer

3

You can return this hierarchy using parent component, looping recursively until you get to Form.

Ex, a button, inside a panel in a form:

uses
  System.Generics.Collections;

function RetornaListaDeParents(aParent: TComponent; aLista: TList<TComponent>): TComponent;
begin
  Result:= nil;
  if aParent <> nil then
  begin
    aLista.Add(aParent);

    if aParent.GetParentComponent <> nil then
      Result:= RetornaListaDeParents(aParent.GetParentComponent, aLista)
    else
      Result:= aParent;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  vLista: TList<TComponent>;
  item: TComponent;
begin
  vLista := TList<TComponent>.Create;
  try
    RetornaListaDeParents(button1, vLista);
    for item in vLista do
    begin
      showmessage(item.Name);
    end;
  finally
    FreeAndNil(vLista);
  end;
end;

Return in list: button, panel, form.

    
09.05.2018 / 20:17