How to obtain hierarchy structure of a Delphi component?

Asked

Viewed 527 times

1

Guys I’m trying to refine my bug log and I came up with an idea.

Using a component ApplicationEvents

I have a routine that records the error log, in it I get the error in E.Message, and the Sender component that generated the error, until then beauty, it happens that sometimes a Objeto does not have the property Name and yes Classename, in this case it is difficult to identify where the error came from, to improve I capture the Active form with "Screen.ActiveControl.UnitName", but sometimes the active form is not the one that generated the error but one that is being created by him... in this case I would have to take the Component that generated the error and go up the hierarchy, so I would know which route I should do to simulate the error in the development.

It also has a function attached 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, because 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 within a Panel->TabSheet->PageControl->Panel->Form.

1 answer

3


It is possible to return this hierarchy using the Parent of the component, making a recursive loop until it reaches the 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 to the list: button, panel, form.

  • 1

    Um interesting... can you tell me if this function would work with non-visual components? For example, the error is in a Query Field, so the hierarchy would be: Field->Query->Form, that is, the component is in a form because it is not Twincontrol.

  • 1

    @Marcelo, I just edited the answer. I lowered a little more in the inheritance, now using the Tcomponent and returning the Parent by the Getparentcomponent method, this way also works for the non-visual components.

  • Thank you very much, that’s right

Browser other questions tagged

You are not signed in. Login or sign up in order to post.