Reputation: 11
I'm using VFI (Visual Form Inheritance) and I need to check if a component of an instantiated form belongs to the formclass or to the form superclass.
any ideas ?
unit1
TFormStatus = class(TForm)
cpPanel: TPanel;
lblStatus: TLabel;
end;
unit 2
TFormCodigo = class(TFormStatus)
lblCodigo: TLabel;
end;
frmCodigo: TFormCodigo:
In any instances of frmCodigo I want to detect that lblCodigo is local to TFormCodigo and cpPanel / lblStatus are inherited components;
for i:=0 to Self.ComponentCount-1 do begin
if "InheritedComponent" (Self.Components[i]) then ...
end;
Something like this is possible using RTTI for object properties, but I dont know if it is possible for components.
Thanks.
Upvotes: 1
Views: 1133
Reputation: 2401
In your TFormCordigo you can override ReadState method that is called every time a resource is read for a particular form. After inherited called ComponentCount contains the number of components created up to the current member of hierarchy, so after all you have list of borders for components that you can save elsewhere.
The code below illustrates this approach
procedure TInhTestForm.Button3Click(Sender: TObject);
var
i: integer;
begin
inherited;
Memo1.Lines.Clear;
for i:=0 to ComponentCount-1 do
begin
Memo1.Lines.Add(format('%s inroduced in %s', [Components[i].Name, ComponentParent(i).ClassName]));
end;
end;
function TInhTestForm.ComponentParent(Index: integer): TClass;
var
i, j: integer;
begin
Result:=Nil;
for i:=Low(fComponentBorders) to High(fComponentBorders) do
begin
if Index <= fComponentBorders[i] - 1 then
begin
j:=i;
Result:=Self.ClassType;
while j < High(fComponentBorders) do
begin
Result:=Result.ClassParent;
Inc(j);
end;
break;
end;
end;
end;
procedure TInhTestForm.ReadState(Reader: TReader);
begin
inherited;
SetLength(fComponentBorders, Length(fComponentBorders) + 1);
fComponentBorders[High(fComponentBorders)]:=ComponentCount;
end;
Upvotes: 0
Reputation: 13991
Maybe something "stupid" like
function TFormStatus.IsStatusComponent(AComponent: TComponent): Boolean;
begin
Result := (AComponent = cpPanel) or (AComponent = lblStatus);
end;
already fulfils your needs?
Upvotes: 0
Reputation: 612794
If I understand you correctly, you need TRttiMember.Parent. For example see this article by Rob Love. You'll need Delphi 2010 or later I think.
In fact this is just part of an excellent series of articles - these articles will also tell you how to get hold of the fields, properties etc. without having to know their names.
Upvotes: 1